designating specific positions for 2 bipolar stepper motors with 4 buttons

Hello all,

I am using an arduino uno, with an adafruit motor shield v1.2, (2) 200 step bi polar stepper motors (http://www.pololu.com/catalog/product/1209) and 4 momentary push buttons with leds (http://www.exp-tech.de/accessories/Button-Switch/Waterproof-Metal-Pushbutton-with-White-LED-Ring-16mm-White-Momentary.html)

I am trying to control 2 stepper motors with 4 different push buttons. Currently, i have written a code that will spin both motors simultaneously (or alternating one step at a time) a certain amount of steps when each button is pressed (code below). However, i need the position the motors spin to, to be more precise. The motors will be attached to a model i am making for an architecture firm, where a smaller ring spins inside of a larger ring in opposite directions, at the same time. There are 4 main positions in which i would like to have the rings move to by pressing a corresponding button. If i were to always press the buttons in order (first button one, then button two, etc) i could set the stepper motors to move a certain number of steps to reach the next position correctly. However, if one would press button 2 and then button 4, the rings won't reach the correct position and from there everything is a mess. I am wondering if there is a way one can set the stepper motor to a specific step so when the button is pressed it returns to that step, or if the buttons can be coded in a way where it recognises the last button that was pressed, and then moves accordingly to the next correct position.

Any and all help is much appreciated. Thanks in advance!!

#include <AccelStepper.h>
#include <AFMotor.h>

const int buttonPin1 = 9;
const int buttonPin2 = 10;
const int buttonPin3 = 13;
const int buttonPin4 = 2;
const int ledPin1 = A1;
const int ledPin2 = A2;
const int ledPin3 = A3;
const int ledPin4 = A4;
AF_Stepper motor1(200,1);
AF_Stepper motor2(200,2);
int x = 0;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  
  // initialize the serial port:
  Serial.begin(9600);
  motor1.setSpeed(300);
  motor2.setSpeed(300);

}
void loop()
{
   if (digitalRead(buttonPin1) == HIGH) 
  {    
  for(int x = 0; x < 200; x++)
  {
  motor1.step(1,FORWARD,SINGLE);
  digitalWrite(ledPin1, HIGH);
  motor2.step(1,BACKWARD,SINGLE);
  }
  }
  else if (digitalRead(buttonPin2) == HIGH) 
  {    
  for(int x = 0; x < 400; x++)
  {
  motor1.step(1,FORWARD,SINGLE);
  digitalWrite(ledPin2, HIGH);
  motor2.step(1,BACKWARD,SINGLE);
  }
  }
else if (digitalRead(buttonPin3) == HIGH) 
  {    
  for(int x = 0; x < 600; x++)
  {
  motor1.step(1,FORWARD,SINGLE);
  digitalWrite(ledPin3, HIGH);
  motor2.step(1,BACKWARD,SINGLE);
  }
  }
  else if (digitalRead(buttonPin4) == HIGH) 
  {    
  for(int x = 0; x < 800; x++)
  {
  motor1.step(1,FORWARD,SINGLE);
  digitalWrite(ledPin4, HIGH);
  motor2.step(1,BACKWARD,SINGLE);
  }
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
      
}
}

I am wondering if there is a way one can set the stepper motor to a specific step so when the button is pressed it returns to that step

No. You can define states, though, and know how many steps, in which direction, to step to get to the desired position for that state, based on which state you were in.

Then, each time a button is pressed, that defines the state to transition to. Since you know the current state, you can simply look up what to do.

PaulS's technique is one of the simplest (hardware wise) techniques available. Don't forget to have the setup find home on powerup. It is, however, possible to throw more hardware (read money) at the issue and know the absolute rotational position of the stepper shafts. One would have to use some sort of absolute rotational encoder. But because there is one sensor per bit of rotational accuracy, they tend to be expensive and have lots of points of failure.

I'd go with PaulS's suggestion.

Thank you both for the help/replies!

Sembazuru, I appreciate the hardware suggestion, but I think you're right, I will try to figure out PaulS's suggestion. I spent the last day or so looking into defining states, but im not sure im finding and/or understanding exactly how to do so. Is it something to do with the Button state? or something entirely different? because it seems all of my google searches lead me to toggling the button state and im not sure how one expands on that.

Thanks again,
Kevin

Is it something to do with the Button state?

No. You just define a varaible, currState, and one called prevState.

When a switch is pressed, set currState to represent which switch was pressed. At the end of loop(), set prevState to currState.

When a switch is pressed, then, you know the current state (you just set it) and the previous state (which you saved earlier).

Suppose that you press switch 3 after pressing switch 2. The current state would be 3. The previous state would be 3. You can then determine how many steps, in which direction, are needed to get to the correct position.

Suppose that you press switch 1 next. The current state would be 1. The previous state would be 2. You can then determine how many steps, in which direction, are needed to get to the correct position.

Simple, no?

wow. very simple! (sounding, we'll see if i succeed ha) but thank you so much PaulS, this is exactly what i need!! youve made my day. XD