# Two stepper one by one

I want to run two stepper motor one after the other Stepper1 has to5 rotation And stepper 2 has to move 1rotation Here is the code

Stepper 1 is nema 34 And stepper 2 is nema 24

``````#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper1 (1, 3, 2); // Defaults to 4 pins on 2, 3, 4, 5 mortor (A)
AccelStepper stepper2 (1, 5, 4); // Defaults to 4 pins on 2, 3, 4, 5 motor (B)

long Steps_For_Distance1 = 3; // 3cm
long Steps_For_Distance2 = 5; // 5cm
long currentPos = 0;
long targetPos = 200;
long targetPos_Reverse = -200;
long Motor_Speed1 = 50;
long Motor_Speed2 = 400;
long Motor_Speed3 = 800;
int Op_Sw = 0;
const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void checkStepper1_For_DT(AccelStepper &astepper)
{
if (astepper.distanceToGo() == 0)
{
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
astepper.setCurrentPosition(currentPos);
astepper.moveTo(targetPos);
astepper.setSpeed(Motor_Speed1);
astepper.setAcceleration(100);
}
}
void checkStepper2_For_360(AccelStepper &astepper)
{
if (astepper.distanceToGo() == 0)
{
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations

astepper.moveTo(targetPos);
astepper.setMaxSpeed(Motor_Speed2);
astepper.setAcceleration(100);
}
}
void PatternToBeLooped_For_Cycle1()
{
int count = 0;

for (count == 0; count <= 5; count ++)
{
checkStepper1_For_DT(stepper1);
stepper1.runSpeedToPosition();
}

checkStepper2_For_360(stepper2);
stepper2.runSpeedToPosition();
}
void loop() {
PatternToBeLooped_For_Cycle1();
}
``````

I had run the code But it didn't work Both motor are running at same time

I had try by delay (1000); But Still they both stepper are moving at a same time and get delay at same time

• please show the wiring diagram Feb 10, 2018 at 16:42
• May it be that they are not moving at the same time but moving one after another very fast, so that you cannot see the difference? Try putting a delay inbetween of the two stepper run statements in your looped function and see what happens Feb 11, 2018 at 12:00
• -Dir pin to arduino gnd Feb 12, 2018 at 2:52
• -Dir pin , - pulse pin to arduino gnd. And + dir to arduino 3 pin ,5pin. And +pulse to pin 2,4 Feb 12, 2018 at 5:06

I looked into the source of the AccelStepper library here, which may be your library or one similar to it. The function `runSpeedToPosition()` does not block until the desired position is reached. Instead inside the function you see code that executes a step, when the right time (according to `millis()`), calculated through the speed, is reached, else it will return false. You are supposed to call this function very often while the motor moves. This enables you to drive more than one motor simultaneously with different speeds.

Since you don't want to drive both motors at the same time, you have to loop, until the first motor have reached the desired position; then doing the same with the second motor. Try something like this:

``````void PatternToBeLooped_For_Cycle1()
{
int count = 0;
for (count == 0; count <= 5; count ++)
{
checkStepper1_For_DT(stepper1);
while(stepper1.currentPosition() != stepper1.targetPosition()) stepper1.runSpeedToPosition();
}

checkStepper2_For_360(stepper2);
while(stepper2.currentPosition() != stepper2.targetPosition()) stepper2.runSpeedToPosition();
}
``````

Note: With this change the function `PatternToBeLooped_For_Cycle1()` blocks the execution of the loop function, until the motors have done their movement. To avoid this you can use an if-statement, which executes the code for stepper1 or stepper2 depending on the `millis()` function. This works the same way as the BlinkWithoutDelay example in the Arduino IDE.