Hello, having a little bit of trouble with my code here. I'd like the stepper motor to not be turning at all if neither momentary button is pressed. But once the first button is pressed, the motor will turn CW. Once released, it will stop. The same goes for the second button just CCW.
Currently, the buttons do what they're supposed to but the motor is constantly turning when neither button is pressed.
/* sketch 1
turn on a LED when the button is pressed
turn it off when the button is not pressed (or released)
*/
#define dirPin 2
#define stepPin 3
#define buttonCW 8
#define buttonCCW 9
void setup() {
pinMode(buttonCW, INPUT); //set the button pin as INPUT
pinMode(dirPin, OUTPUT); //set the LED pin as OUTPUT
pinMode(buttonCCW, INPUT); //set the button pin as INPUT
pinMode(stepPin, OUTPUT); //set the LED pin as OUTPUT
}
void loop()
{
if(digitalRead(buttonCCW))
digitalWrite(dirPin, HIGH);
else if(digitalRead(buttonCW))
digitalWrite(dirPin, LOW);
else
{
// move
}
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
Any help is appreciated.