Hi all!
I'm putting together a circuit that when an IR code is recieved it activates a stepper motor to turn 90 degrees.
I want the stepper motor to have two positions, either up or down. If it is down, I want it to be only able to go back up (Or else it would ram into the frame) and if it is in the up position, I only want it to go down (for the same reasons). The physical limits are 0 degrees and 90 degrees.
I found this excellent tutorial online from Brainybits: https://brainy-bits.com/blogs/tutorials/control-stepper-motor-with-arduino
And I have successfully assembled it and tested it. However I want to add my "safety" to it so that if a button is accidently pressed the motor doesn't turn in the same direction a second time. Which is why I am trying to incorporate two micro-switches as stops. In my code I have them plugged into digital pins 4 and 3 (along with GND). I am using the same board as in the tutorial the Uno R3.
I have spent a while searching the interwebs and have found a few examples that come close but nothing exactly what I need.
I had a crack at adding my own bits of code and -surprise- it doesn't work!
#include <IRremote.h>
#include <Stepper.h>
/*----- Variables, Pins -----*/
#define STEPS 32 // Number of steps per revolution of Internal shaft
int Downlocked = 3; // Antenna down microswitch
int Uplocked = 4; // Antenna up microswitch
int Steps2Take; // 2048 = 1 Revolution
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
int stepCount = 0; // number of steps the motor has taken
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFF629D: // UP button pressed
if (Downlocked)==HIGH)
{
small_stepper.setSpeed(500); //Max seems to be 700
Steps2Take = -512; // Rotate CCW
small_stepper.step(Steps2Take);
delay(2000);
break;
}
else if (Uplocked)==HIGH)
{
small_stepper.setSpeed(500); //Max seems to be 700
Steps2Take = 512; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
break;
}
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
I think that by running it like this (or something similar) I can actually get away with using only one button on the IR remote.
Thanks for any help or advise, it is much appreciated!
if (Downlocked)==HIGH)
Downlocked = 3. So that if will always be HIGH. Maybe you need digitalRead(Downlocked) there?
else if (Uplocked)==HIGH)
Same here.
When the microswitch is activated, does it open a circuit or close it, how are the switches connected to the Arduino?
groundFungus:
if (Downlocked)==HIGH)
Downlocked = 3. So that if will always be HIGH. Maybe you need digitalRead(Downlocked) there?
else if (Uplocked)==HIGH)
Same here.
Thanks for replying!
Okay.. So because I specify that it is 3 it will always be high? Okay that might be a mistake. I was trying to tell it that it was connected to digital pin 3.
DigitalRead! Okay, my knowlege is quite limited haha, but that makes sense! DigitalRead tells the board which pin to look at correct?
edgemoron:
When the microswitch is activated, does it open a circuit or close it, how are the switches connected to the Arduino?
When the microswitch is activated it closes the circuit. Microswitch 1 (The one telling it that the motor is in the down position) is connected to pin 3 and Microswitch 2 (The one telling the motor that it is in the up position) is connected to pin 4. Both are also connected to GND.
So, in setup(), you need:
void setup()
{
pinMode(Downlocked,INPUT_PULLUP);
pinMode(Uplocked,INPUT_PULLUP);
Then:
if(digitalRead(Downlocked) == LOW) // down switch is pressed
Okay.. so like this, or are they in the wrong place.. I think they are because I am still getiing error messages.
Also looking at it again, I think I could nearly get away with only using one microswitch (maybe).
#include <IRremote.h>
#include <Stepper.h>
/*----- Variables, Pins -----*/
#define STEPS 32 // Number of steps per revolution of Internal shaft
int Downlocked = 3; // Antenna down microswitch
int Uplocked = 4; // Antenna up microswitch
int Steps2Take; // 2048 = 1 Revolution
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
int stepCount = 0; // number of steps the motor has taken
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
pinMode(Downlocked,INPUT_PULLUP);
pinMode(Uplocked,INPUT_PULLUP);
}
void setup()
{
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFF629D: // UP button pressed
if(digitalRead(Downlocked) == LOW) // down switch is pressed
{
small_stepper.setSpeed(500); //Max seems to be 700
Steps2Take = 512; // Rotate CCW
small_stepper.step(Steps2Take);
delay(2000);
break;
}
else if digitalRead(Downlocked)==HIGH)
{
small_stepper.setSpeed(500); //Max seems to be 700
Steps2Take = -512; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
break;
}
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
void setup()
{
pinMode(Downlocked,INPUT_PULLUP);
pinMode(Uplocked,INPUT_PULLUP);
}
void setup()
{
irrecv.enableIRIn(); // Start the receiver
}
You can have only one setup() function.
Try:
void setup()
{
pinMode(Downlocked,INPUT_PULLUP);
pinMode(Uplocked,INPUT_PULLUP);
irrecv.enableIRIn(); // Start the receiver
}
else if digitalRead(Downlocked) == HIGH)
Mind your parenthesis. Should be:
else if (digitalRead(Downlocked) == HIGH)
Compiles now with IDE 1.6.7 Win10.
How about a picture of that contrivance. 
You guys are legends!!
It compiles and runs perfectly, thank you very much for your help!! 
As for your request.. this is what my arduino is going into:

My ARC trooper helmet from Star Wars legends.
As you can see there is a slot cut out of the right hand ear piece, this is where the "Antenna" (next to the helmet) will mount. With the press of a button on my remote I can drop it down infront of the visor and with another press it returns to the up position.
I will remove the insides of the remote and mount it into my forearm armour. It should look pretty cool when it is done!
Maybe I can post the link to a video of it working when I get it finished.
Thanks again, you guys are awesome.
Thanks, good luck & have fun.