Driving 2 Stepper Motors at the same time

Hi,
I'm trying to run 2 stepper motors at the same time using an Adafruit Motor Shield. I have managed to get the motors to move in both directions but not at the same time. how can I get both to move at the same time?
Here is the sketch i'm using;

#include <AFMotor.h>
int start = 0;

// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;


AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Stepper test!");
  motor1.setSpeed(95);  // 85 rpm
  motor2.setSpeed(95);  // 85 rpm
  pinMode(13, OUTPUT);
}

void swip()
{
  Serial.println("Double coil steps");
  motor1.step(430, BACKWARD, DOUBLE);
  motor2.step(430, BACKWARD, DOUBLE);
  delay(1500);
  motor1.step(430, FORWARD, DOUBLE);
  motor2.step(430, FORWARD, DOUBLE);
  start++;

}
void loop() {

  if (start == 0)
    swip();
  delay(500);
  digitalWrite(13, HIGH);
}
  for (int i = 0; i < 430; i++)
  {
    motor1.step(1, BACKWARD, DOUBLE);
    motor2.step(1, BACKWARD, DOUBLE);
  }
1 Like

Can i be stupid and ask what to replace?

never used the AFMotor.h-library
if the motors do not move at the same time your code

does: run motor1 for 430 steps and do nothing else than create the 430 step-pulses until the 430th-pulse is created
after the 430 pulses for motor 1 have been created
after that
do:
run motor2 for 430 steps and do nothing else than create the 430 step-pulses until the 430th-pulse has been created

The code that user @johnwasser posted

does the following
motor1: create one single step then finsish function motor1.step(1, BACKWARD, DOUBLE)
motor2: create one single step then finsish function motor2.step(1, BACKWARD, DOUBLE)

and this pattern
create one step for motor1
directly after that
create one step for motor2

is repeated 430 times through the for-loop

best regards Stefan

This is what I did, but the motors are no longer moving;

#include <AFMotor.h>
int start = 0;

// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;


AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Stepper test!");
  motor1.setSpeed(85);  // 85 rpm
  motor2.setSpeed(85);  // 85 rpm
  for (int i = 0; i < 430; i++);
  }

void swip()

{

  motor1.step(1, BACKWARD, DOUBLE);
  motor2.step(1, BACKWARD, DOUBLE);
  delay(500);
  motor1.step(1, FORWARD, DOUBLE);
  motor2.step(1, FORWARD, DOUBLE);
  start++;

}
void loop() {

  if (start == 0)
    swip();
 }

This doesn't do anything by itself.

If you want to step 430 steps instead of 1 you have to put loops around:

  motor1.step(1, BACKWARD, DOUBLE);
  motor2.step(1, BACKWARD, DOUBLE);

and

  motor1.step(1, FORWARD, DOUBLE);
  motor2.step(1, FORWARD, DOUBLE);

Also, not in this case, be careful placement of

;

for (int i = 0; i < 430; i++);

this code

does count up variable i from 0 to 429 and thats all
.
.
.
.
.
.
this code

for (int i = 0; i < 430; i++) {
  // repeat 430 times
  motor1.step(1, BACKWARD, DOUBLE); // drive motor 1 step
  motor2.step(1, BACKWARD, DOUBLE); // drive motor 1 step
}

does count up variable i from 0 to 429 and

additionally

create

single

step-pulses

to make it more visible for you

for (int i = 0; i < 430; i++) {
  // repeat 430 times
  motor1.step(5, BACKWARD, DOUBLE); // drive motor1     5 steps
  motor2.step(5, BACKWARD, DOUBLE); // drive motor2     5 step
}

motor1.step(5, BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5, BACKWARD, DOUBLE); // drive motor2 5 step

You should start reading code very slow and very carefully instead overflying the code at supersonic speed

Please tell me what I'm doing wrong. My programming skills are very limited.

#include <AFMotor.h>
int i = 0;


// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;


AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Stepper test!");
  motor1.setSpeed(85);  // 85 rpm
  motor2.setSpeed(85);  // 85 rpm
  for (int i = 0; i < 430; i++) // repeat 430 times
  motor1.step(5, BACKWARD, DOUBLE); // drive motor1     5 steps
  motor2.step(5, BACKWARD, DOUBLE); // drive motor2     5 step
  
}
  

void swip()

{
  
  Serial.println("Double coil steps");
  motor1.step(5, BACKWARD, DOUBLE);
  motor2.step(5, BACKWARD, DOUBLE);
  
 i++;

}
void loop() {

  if (i = 0)
    swip();
 }

Cannot help with other things, but isn't this

  for (int i = 0; i < 430; i++) // repeat 430 times
  motor1.step(5, BACKWARD, DOUBLE); // drive motor1     5 steps
  motor2.step(5, BACKWARD, DOUBLE); // drive motor2     5 step
  

supposed to be this (like @StefanL38 showed)? Notice the curly braces { } ?

for (int i = 0; i < 430; i++) {
  // repeat 430 times
  motor1.step(5, BACKWARD, DOUBLE); // drive motor1     5 steps
  motor2.step(5, BACKWARD, DOUBLE); // drive motor2     5 step
}

Yes I will tell you:
You should improve your programming-knowledge by reading an easy to understand tutorial.

I recommend this one:
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

Whenever you have questions about a line of code in this tutorial for sure you can ask all these questions here.

best regards Stefan

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.