Stepper Motor with Push Buttons

Hi everyone!

I'm facing some trouble with my project, it's basically a stepper motor connected to the big easy driver and onto the arduino board with two push buttons for forward and reverse motion (BRed and BWhite, respectively). When I execute, the motor automatically rotates when connected to the power supply. However, this only happens when I include the else if code for the reverse motion. When it is excluded, the motor waits for my input (BRed). Can anyone help?

This is my code.

//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define MS3 6
#define EN  7
#define BRed 8
#define BRedenabler 9
#define BWhiteenabler 10
#define BWhite 11

//Declare variables for functions
char user_input;
int x,u,v;
int y;
int state;

void setup() {
 pinMode(stp, OUTPUT);
 pinMode(dir, OUTPUT);
 pinMode(MS1, OUTPUT);
 pinMode(MS2, OUTPUT);
 pinMode(MS3, OUTPUT);
 pinMode(EN, OUTPUT);
 pinMode(BRed,INPUT);

pinMode(BRedenabler,OUTPUT);
digitalWrite(BRedenabler, HIGH);

 pinMode(BWhite,INPUT);
 pinMode(BWhiteenabler,OUTPUT);
digitalWrite(BWhiteenabler, HIGH);
 resetBEDPins();
  Serial.begin(9600);
  
}

//Main loop
void loop() {
digitalWrite(EN, LOW);
u = digitalRead(BRed);
v = digitalRead(BWhite);
Serial.println(u);

 digitalWrite(MS1, HIGH);
 digitalWrite(MS2, HIGH);
 digitalWrite(MS3, HIGH);


if (u==HIGH) {
StepForwardDefault();
}

else if (v==HIGH) {
 ReverseStepDefault();
}

resetBEDPins();

}

//Reset Big Easy Driver pins to default states
void resetBEDPins()
{
 digitalWrite(stp, LOW);
 digitalWrite(dir, LOW);
 digitalWrite(MS1, LOW);
 digitalWrite(MS2, LOW);
 digitalWrite(MS3, LOW);
 digitalWrite(EN, HIGH);
}

//Default microstep mode function
void StepForwardDefault()
{
 digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
 for(x= 1; x<2; x++)  //Loop the forward stepping enough times for motion to be visible
 {
   digitalWrite(stp,HIGH); //Trigger one step forward
   delay(1);
   digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
   delay(1);
 }
}

//Reverse default microstep mode function
void ReverseStepDefault()
{
 
 digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
 for(x= 1; x<2; x++)  //Loop the stepping enough times for motion to be visible
 {
   digitalWrite(stp,HIGH); //Trigger one step
   delay(1);
   digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
   delay(1);
 }
 
}

You declare 'BRed' as INPUT - do you have it wired up with a pull-up or pull-down resistor? You will need one. You can also use the internal pull-up resistors if you enable them. Hard to tell without a schematic.

MahmoudHH:
Hi everyone!

Welcome to the Forum.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code for easier reading.

...R
Stepper Motor Basics
Simple Stepper Code