I need a little help to run my two stepper motors.

I am running two stepper motors and want stepper1 to move to a certain position and then stepper2 to move to a certain position with the press of a button. This is the code I have used so far:

#include <AccelStepper.h>

// Define some steppers and the pins the will use
//AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper1(AccelStepper::FULL2WIRE, 8, 9);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 10, 11);
int ButtonState = 0;
int OnOrOffState = 0;
void setup()
{  
    stepper1.setMaxSpeed(700.0);
    stepper1.setAcceleration(200.0);
    stepper1.moveTo(10000);
    
    stepper2.setMaxSpeed(300.0);
    stepper2.setAcceleration(200.0);
    stepper2.moveTo(4000);
    
    
}

void loop() {

 ButtonState = digitalRead(2);
if (ButtonState == LOW){
  OnOrOffState = 1;}

while(OnOrOffState ==1){ 
  stepper1.run();
   
    if (stepper1.distanceToGo() == 0)
   
    stepper2.run();
   


if (stepper2.distanceToGo() == 0){
    OnOrOffState = 0;
 
}
}
}

This seems to work but what I want to do next is be able to press the same button again and it run in reverse.
stepper2 to return to it's original position followed by stepper1.
Can you help me with this part of the code please.

I am using an Arduino Uno with two HY-DIV268N-5A drivers and two Nema 23 motors. 24v power supply.

and two Nema 23 motors

First question. Do you have any idea what NEMA 23 means?

Second question. If you only want one stepper at a time to move, why are you using AccelStepper?

Third question. So, what is the problem?

int OnOrOffState = 0;

Please don't use stupid names like this. A boolean holds true or false. Something is either on or off. A perfect match.

bool onState = true; // The device is on.

onState = false; // Now, it isn't.

There is nothing about OnOrOffState that implies what is on or off, or what value means on or off.

Put every { on a new line. Use Tools + Auto Indent to properly format your code.

Look at the state change detection example. It shows how to deal with the switch BECOMING pressed (versus BEING pressed). The action to perform when the number of times that the switch has BECOME pressed is odd is one thing. The action to perform when the count is odd is something else.

Using the Stepper library, instead of AccelStepper, makes moving one motor and then the other so much simpler.

Firstly let me answer your questions.
Question 1. Nema 23 are the stepper motors I am using for my project.
Question 2. I don't know. I am a complete novice and that was the advice I was given.
Question 3. I don't know how to write the code.

I am using pins 8 & 9 for the first stepper motor on my arduino uno and pins 10 & 11 for the second.
My push button is connected to pin 2.
I am happy with all of my wiring, it is just the code I am struggling with.

Thank you for your interest and advice and I would be grateful for more if you have the time.

OK. After following your advice i have started again.
Here is the code so far.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9);            
Stepper myStepper2(stepsPerRevolution, 10,11);


void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  myStepper2.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:

  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  myStepper2.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper2.step(-stepsPerRevolution);
  myStepper.step(-stepsPerRevolution);
  delay(500); 
}

I need the steppers to do a lot more revolutions and smoother, this may involve microsteps.
Also I need the program for the push button.
Can you help.

OK, I'm getting there.
Now I just need to insert the code for the button.
I want to press the button and it moves stepper 1 and 2 to there locations and stop. when I press the button again I want them to return.
Here is the code I have so far.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9);            
Stepper myStepper2(stepsPerRevolution, 10,11);


void setup() {
  
  // set the speed at 60 rpm:
  myStepper.setSpeed(1200);
  myStepper2.setSpeed(600);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:

  Serial.println("counterclockwise");
  myStepper.step(-20000);
  Serial.println("clockwise");
  myStepper2.step(5000);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper2.step(-5000);
  Serial.println("clockwise");
  myStepper.step(20000);
  delay(500); 
}

So, you problem is not steeper motors, your problem is how to make a sketch keep track of doing thing A and thing B alternately when a button is pressed. IN your case, A and B are "move the steppers up" and "move the steppers back".

You want something like this:

enum StepperState {
  START_POSITION = 0,
  EXTENDED_POSITION = 1
} stepperState;

const int BUTTON_PIN = 7;
byte buttonState;

void setup() {
  // put your setup code here, to run once:

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  stepperState = START_POSITION;
  buttonState = HIGH;
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  byte previousButtonState = buttonState;
  buttonState = digitalRead(BUTTON_PIN);
  boolean buttonPress = previousButtonState == HIGH && buttonState == LOW;

  switch(stepperState) {
    case START_POSITION:
    if(buttonPress) {
      Serial.println("Extend the stepper!");
      stepperState = EXTENDED_POSITION;
    }
    break;

    case EXTENDED_POSITION:
    if(buttonPress) {
      Serial.println("Retract the stepper!");
      stepperState = START_POSITION;
    }
    break;
  }

}

I have made "stepper state" an enum rather than a boolen because - sure as anything - you will eventually want to make it more complex :slight_smile: .

Your next problem will be debouncing the button, but I'll let you find out about that on your own.

Firstly let me answer your questions.
Question 1. Nema 23 are the stepper motors I am using for my project.

The NEMA 23 standard defines mounting hole locations. How do you suppose that that is important information?

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9);           
Stepper myStepper2(stepsPerRevolution, 10,11);

If you are going to number one variable in a set, number ALL of them.

But, really, wouldn't panMotor and tiltMotor make more sense (or whatever the two motors actually do)?

So this is what I have ended up with.
Thank you for all your help.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


Stepper panmotor(stepsPerRevolution, 8,9);            
Stepper tiltmotor(stepsPerRevolution, 10,11);

enum StepperState {
  START_POSITION = 0,
  EXTENDED_POSITION = 1
} stepperState;

const int BUTTON_PIN = 2;
byte buttonState;


void setup() {
  
  // set the speed rpm:
  panmotor.setSpeed(1500);
  tiltmotor.setSpeed(600);
  pinMode(BUTTON_PIN, INPUT);
  stepperState = START_POSITION;
  buttonState = HIGH;
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
 
  byte previousButtonState = buttonState;
  buttonState = digitalRead(BUTTON_PIN);
  boolean buttonPress = previousButtonState == HIGH && buttonState == LOW;

  switch(stepperState) {
    case START_POSITION:
    if(buttonPress) {
      Serial.println("Extend the stepper!");
      // step panmotor counterclockwise at the pus of a button followed by tiltmotor in a clockwise direction:
  Serial.println("counterclockwise");
  panmotor.step(-20000);
  Serial.println("clockwise");
  tiltmotor.step(5000);
  delay(500);
      stepperState = EXTENDED_POSITION;
    }
    break;
 
 case EXTENDED_POSITION:
    if(buttonPress) {
      Serial.println("Retract the stepper!");
      // return tiltmotor counterclockwise at the pus of a button followed by panmotor in a clockwise direction:
  Serial.println("counterclockwise");
  tiltmotor.step(-5000);
  Serial.println("clockwise");
  panmotor.step(20000);
  delay(500); 
      stepperState = START_POSITION;
    }
    break;
  }
}

GeoffBowes-Smith:
So this is what I have ended up with.
Thank you for all your help.

Alter the title of your post and add '[FIXED]' or '[DONE]' or '[COMPLETE]' or something to the end of it.

And don't forget to hit the karma button or those posts that were helpful.