Writing Code to Control Stepper Motor

Hello, I am very new to this and am looking for a bit of help on writing code for a stepper motor to be controlled by four push button switches. I want the pressing of button one to move the stepper motor to 0 degrees, the pressing of button 2 to move it to 90 degrees (400 micro steps), the pressing of button 3 to move it to 180 degrees (800 micro steps), and the pressing of button 4 to move it to 270 (1200 micro steps). Basically 90 degree, or 400 micro step increments from a start position, each button after button one adding 90 degrees (400 micro steps) to the previous button position. I have an Arduino Mega 2560 R3 and am using an EasyDriver Stepper Motor Driver from Sparkfun. I have attached both diagrams, and also the code I have written so far.

Again, I am incredibly novice at this, and literally any help is welcome.

Is this right…?

//In this sketch I wish to control a stepper motor using my Arduino Mega 2560 R3, utilizing four push button switches.

#define DIR 22 //setting the direction output of stepper (DIR) to pin 22
#define STP 2  //setting steps output of stepper motor (STP) to pin 2
#define PB1 25 //setting input of push button switch one (PB1) to pin 25
#define PB2 26 //setting input of push button switch two (PB2) to pin 26
#define PB3 27 //setting input of push button switch three (PB3) to pin 27
#define PB4 28 //setting input of push button switch four (PB4) to pin 28

void setup()
{
  pinMode(DIR, OUTPUT);
  pinMode(STP, OUTPUT);
  pinMode(PB1, INPUT);
  digitalWrite(PB1, HIGH);
  pinMode(PB2, INPUT);
  digitalWrite(PB2, HIGH);
  pinMode(PB3, INPUT);
  digitalWrite(PB3, HIGH);
  pinMode(PB4, INPUT);
  digitalWrite(PB4, HIGH);
}
void loop()
{
  byte a=!digitalRead(PB1);
  byte b=!digitalRead(PB2);
  byte c=!digitalRead(PB3);
  byte d=!digitalRead(PB4);
  int stepperPosition0=0;
  
  if (a==1)                                                          //if PB1 is pressed
  {
    digitalWrite(DIR,LOW);                                           //set the direction
    delay(100);
    
    for(stepperPosition0=0; stepperPosition0<0;stepperPosition0++)   //pulse for 0 microsteps, 0 degrees of rotation
    {
      digitalWrite(STP,HIGH);
      digitalWrite(STP,LOW);
      delay(500);
    }
  }
  else if (b==1)                                                     //unless PB2 is pressed
  {
    digitalWrite(DIR,LOW);                                           //set the direction
    delay(100);
    
    for(stepperPosition0=0; stepperPosition0<400;stepperPosition0++) //pulse for 400 microsteps, 90 degrees of rotation
    {
      digitalWrite(STP,HIGH);
      digitalWrite(STP,LOW);
      delay(500);
    }
  }
  else if (c==1)                                                     //unless PB3 is pressed
  {
    digitalWrite(DIR,LOW);                                           //set the direction
    delay(100);
    
    for(stepperPosition0=0; stepperPosition0<400;stepperPosition0++) //pulse for 400 microsteps, 90 degrees of rotation
    {
      digitalWrite(STP,HIGH);
      digitalWrite(STP,LOW);
      delay(500);
    }
  }
  else if (d==1)                                                     //unless PB4 is pressed
  {
    digitalWrite(DIR,LOW);                                           //set the direction
    delay(100);
    
    for(stepperPosition0=0; stepperPosition0<400;stepperPosition0++) //pulse for 400 microsteps, 90 degrees of rotation
    {
      digitalWrite(STP,HIGH);
      digitalWrite(STP,LOW);
      delay(500);
    }
  } 
}

I have attached both diagrams,

Where?

Is the code correct for the task I explained? I cannot test it on my board currently, and I need to know before I continue with this sketch. Some outside opinions would be appreciated, thanks!

Is the code correct for the task I explained?

Maybe you and this other dude could get together and collaborate. You two seem to be having the identical issues:

PS. His code was not very good, so take it with a grain of salt.

Maybe you and this other dude could get together and collaborate.

Haha... A good idea in theory, except for the fact that the "other dude" is me, and it'd be a it tough to collaborate with myself. That first version of code is quite poor, I agree. I'm still in high school, have never taken a coding class, have no experince with code, and haven't even received my arduino in the mail yet... I'm shooting in the dark. I'm hoping this second version of code is better, could someone tell me if it would carryout the task described?

Just looking for some guidance, thank you.

The nots are still there. The comparisons to 1, instead of HIGH or LOW are still there.

    for(stepperPosition0=0; stepperPosition0<0;stepperPosition0++)   //pulse for 0 microsteps, 0 degrees of rotation

Why? If there is nothing to do, a 0 iteration loop is NOT the way not to do it.

What is going to happen when the stepper has rotated 180 degrees, and you press the 1st switch? What should happen?

In my mind, the stepper should rotate back to 0, not do nothing.

What is going to happen when the stepper has rotated 180 degrees, and you press the 1st switch? What should happen?

In my mind I also want the stepper to return to 0, but I really don't know how to write the code for that... could you help?

You need to keep track of where the stepper is. Then, when a switch is pressed to make it move, you need to determine the direction and number of steps.

If you are at 90 degrees, and switch 4 is pressed, you should go to 270 degrees (You need 5 switches for a complete rotation). That is a difference of +180 degrees, so set the direction to positive and step (180/90 * 400) times. Record the new position (270/90 * 400).

If you are at 270 and switch 2 is pressed, you should go to 90 degrees. That is a difference of -180 degrees, so set the direction to negative and step (180/90 * 400) times. Record the new position (90/90 * 400).

Is there a way to autonomously do all of this? I bought a stepper for its high degree of precision compared to a servo, the trouble with a stepper is they don’t know “where they are” as a servo does, a stepper can just continue rotating and rotation with no feedback of where it actually is.

Because it doesn’t know where it is, I am confused on how to tell it to move back and forth… I want button two to move it to 90 degrees, button 3 to 180, and if I press button two again I want it back to 90 degrees. Every situation is different, and I’m unsure how to write the code so that it “knows” where each position is so that it can return back if necessary.

Thank you Paul, it’s incredibly helpful to have someone replying, I really do appreciate it.

When driving a stepper motor you normally want to keep track of the current position (number of net clockwise steps since power-up or some such).

So that's one variable, lets call it "actual_position". Then you will have code that responds to user input and determines where you want the thing to be - call this "target_position".

Your code can then neatly be divided into:

A) the part that interprets user actions like button presses and updates target_position
B) the part that looks at actual_position and target_position and determines whether to step and if so
which way - this part can also handle any acceleration / velocity issues. When you actually step
actual_position is updated accordingly.

Your code can then neatly be divided into:

A) the part that interprets user actions like button presses and updates target_position
B) the part that looks at actual_position and target_position and determines whether to step and if so
which way - this part can also handle any acceleration / velocity issues. When you actually step
actual_position is updated accordingly.

That's pretty much exactly what I'm looking to do, thank you! The problem is, is that I am pretty much absolutely clueless as to how to write that code... I have nothing to study from on all of this, examples are how I learn these types of things best.

examples are how I learn these types of things best.

Odd that, an example of exactly what you want to do is often the easy way of doing anything.

Odd that, an example of exactly what you want to do is often the easy way of doing anything.

There is no need to comment if you're not looking to help, and there is no need to criticize me or how I phrased something when you have not offered anything yourself. I don't know how to write code, plain and simple. The lines of code I have posted in this forum post are the only lines of code I have written in my life. Yes, it is helpful to have something done correctly to key off of when trying to learn a new process. The stepper control commands in my sketch, I learned those from a tutorial posted by SparkFun. The "#define" lines and general setup commands I learned how to do from youtube. The "for" loops I learned how to do from the very Arduino site in their references section. I'm not asking anyone to write my code for me, but in some of these I am reading jargon of programming that I do not understand, so yes, I asked for a code example. An example of code does not mean their using my values or writing it to my specifications, it's simply a correctly formatted template. This forum is about people who need help solving problems, so please do not criticize the way in which I want to find a solution, it's unnecessary.

We get many people here that are just trying to get some one else to do their work for them and that is the sort of thing they say.
If that is not where you are at and really want help to learn there are lots of people here, including myself willing to help you.

So you started off with some code, asked a question and got a bunch of suggestions. Now what we are expecting from you is something like - I tried that and this is what I did - code you wrote posted - but when I run it what happens is xxxxxxx where I expected it to be yyyyyyyy.
Then we can help. Keep posting your code because as you change it new aspects will crop up. It also shows us that you are trying to learn and take on board what we told you not you are just lazy and want someone to do the work for you.

Do not try and do something complex straight off, do it a step at a time, test that step before you go onto the next stage. Learning to break a task down into manageable chunks is just as difficult initially as writing the code but it is a skill that pays dividends.