Stepper Motor Control Using Four Push Buttons (Help)

Hi
I need help :confused: . I want to control a Nema17 stepper motor using an Easydriver and four push buttons.

Button 1 - Continuous forward
Button 2 - Stop
Button 3 - Stepped forward (See below)
Button 4 - continuous reverse

Basically it is to run an 8mm film projector project.

Could I please get help with the code to make this work? I’m a real novice and need help with the code for this.

Stepped forward Details:

int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin

void setup(){
/Sets all pin to output; the microcontroller will send them(the pins) bits, it will not expect to receive any bits from thiese pins./
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);

Serial.begin(9600);
}

void loop(){
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/moderatly turns the motor 4800 steps - three revolutions/
for (int i = 0; i < 4800; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(130);
digitalWrite(smStepPin, LOW);
delayMicroseconds(130);
}

delay(500); //The motor pauses for a 0.5 seconds (the motor does not need to pause between switching direction, so you can safely remove this)

digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (High is clockwise).
/Moderatley turns the motor 4800 steps - three revolutions/
for (int i = 0; i < 4800; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(130);
digitalWrite(smStepPin, LOW);
delayMicroseconds(130);
}

delay(500); //The motor pauses for a 0.5 seconds
}

If you need to be able to stop a movement in the middle then you should NOT use the delay() function or a FOR loop because both block the Arduino until it completes and prevent it from responding to another button press.

Just use a variable to keep count of the steps and allow loop() to do the repetition. That way your code can check the buttons between each step.

And use millis() and micros() for non-blocking timing. Have a look at the second example in this Simple Stepper Code

To make it easier for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R
Stepper Motor Basics