# Using two Stepper Motors simultaneously with different Speeds (in Sync)

Hi Everyone,

for my Project I need to move 2 Steppermotors at different Speeds and Distances but both need to stop at the same time after a certain amount of steps.
I tried a lot with Accelstepper, but both motors just either move at the same speed OR move at different speeds but not in sync (if i choose twice the Speed of MotorA and twice the distance, they don’t stop at the same time).

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

#define MOTOR_A_ENABLE_PIN 8
#define MOTOR_A_STEP_PIN 4
#define MOTOR_A_DIR_PIN 7

#define MOTOR_B_ENABLE_PIN 8
#define MOTOR_B_STEP_PIN 3
#define MOTOR_B_DIR_PIN 6

AccelStepper motorA(1, MOTOR_A_STEP_PIN, MOTOR_A_DIR_PIN);
AccelStepper motorB(1, MOTOR_B_STEP_PIN, MOTOR_B_DIR_PIN);

void setup()
{
motorA.setMaxSpeed(800);
motorA.setAcceleration(3200);
motorB.setMaxSpeed(400);
motorB.setAcceleration(3200);
}

void loop(){
motorA.moveTo(3200);
motorB.moveTo(1600);

motorA.run();
motorB.run();

}
``````

I don’t necessary need an accleration, but it would be a nice to have.
I read about the MultiStepper Library, but all examples aren’t working with my setup. I thought MultiStepper would be an option, because according to the Documentation it can move Motors so, that they arrive at the same time. But how do I code this? How can I set up the speed of one motor?

Does anyone has an idea, how to solve my Problem?

Here a little example, how i thought it should work:

MotorA moves 3000 Steps at a certain Speed
MotorB moves x3000 Steps at xSpeed_MotorA
->Both move simultaneously and stop at the same time after completing their steps.

The Stepper library moves one stepper at a time, at constant speed, in a blocking fashion.

The AccelStepper library moves any number of steppers at the same time, at varying speeds, but the movements are NOT in sync.

The MultiStepper library moves any number of steppers at the same time, but assures that they all complete their movements at the same time.

So, you are using the wrong library to achieve your goal.

Post the code you tried using the MultiStepper library.

...R

I honestly tried to understand the MultiStepper library, but I don’t think I get it.
This is what I tried, but I got the error: “addStepper was not declared”- whats wrong with my code?

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

#define MOTOR_A_ENABLE_PIN 8
#define MOTOR_A_STEP_PIN 4
#define MOTOR_A_DIR_PIN 7

#define MOTOR_B_ENABLE_PIN 8
#define MOTOR_B_STEP_PIN 3
#define MOTOR_B_DIR_PIN 6

AccelStepper motorA(1, MOTOR_A_STEP_PIN, MOTOR_A_DIR_PIN);
AccelStepper motorB(1, MOTOR_B_STEP_PIN, MOTOR_B_DIR_PIN);

void setup()
{
}

void loop(){
motorA.runSpeedToPosition(2000);
motorB.runSPeedToPosition(1000);
}
``````

As far as I understood this so far, it should move both at a different Speed, so both positions will be reached at the same time (Which is exatly what I would like to achieve). But how can I set at least one of the speeds?

If it’s important: I’m using a CNC Shield V3 and Pololu A4988 Driver

whats wrong with my code?

Did you look at the examples that came with the MultiStepper library? I would expect that you would need to create an instance of the MultiStepper class, and tell that instance to add the AccelStepper instances.

I worked on my Code, according to the example given.

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

#define MOTOR_A_ENABLE_PIN 8
#define MOTOR_A_STEP_PIN 4
#define MOTOR_A_DIR_PIN 7

#define MOTOR_B_ENABLE_PIN 8
#define MOTOR_B_STEP_PIN 3
#define MOTOR_B_DIR_PIN 6

AccelStepper motorA(1, MOTOR_A_STEP_PIN, MOTOR_A_DIR_PIN);
AccelStepper motorB(1, MOTOR_B_STEP_PIN, MOTOR_B_DIR_PIN);

void setup()
{
motorA.setMaxSpeed(2000);
motorB.setMaxSpeed(2000);
}

void loop(){
long positions[2];
positions[0]=1000;
positions[1]=500;
steppers.moveTo(positions);   <-ERROR
steppers.runSpeedToPosition();
delay(1000);
}
``````

But still, I get an error: “steppers was not declared” - Why?

Compare your code in Reply #5 with the code in the multstepper example

...R

I found my Problem ::) Thanks for your help!

I just got one more question: Is there an opportunity to stop both motors immediately? Like an emergency stop. I tried it with:

``````motorA.stop();
motorB.stop();
``````

But they didn't stop right away.

I tried it with

some code put who knows where

But they didn't stop right away.

I can't imagine why.

Yoink:
I tried it with:

``````motorA.stop();
``````

motorB.stop();

``````

But they didn't stop right away.
``````

As you have not posted the program where you tried that we cannot know what exactly you did.

…R