Go Down

Topic: Running two steppers at the same time (Read 771 times) previous topic - next topic

Sajuuk

Hello,
I am attempting to use an arduino to drive two steppers at the same time. The setup is relatively simple: each stepper is connected to an ULN2003(one ULN2003 for each stepper) and then to the Arduino. Each motor runs fine. The problem is that they don't work at the same time.
The first stepper moves, it stops, and then the second one starts to move and so on.
How can I get the steppers to move at the same time ?

I tried modifying the code from the examples, OneRevolution (included with Arduino IDE v1.0)
Code: [Select]

#include <Stepper.h>
const int stepsPerRevolution = 100;
const int stepsPerRevolution2 = 50;

Stepper myStepper(stepsPerRevolution,8,9,10,11);           
Stepper myStepper2(stepsPerRevolution2,4,5,6,7); 
void setup() {
  myStepper.setSpeed(200);
  myStepper2.setSpeed(50);

}

void loop() {

   myStepper.step(stepsPerRevolution);
   myStepper2.step(stepsPerRevolution);
  delay(500);

  myStepper.step(-stepsPerRevolution);
  myStepper2.step(-stepsPerRevolution);
  delay(500);
}

John_S

The Stepper library is blocking, which means the step() command has to finish the steps before the program resumes.

A couple solutions:
1. Step each motor one step at a time:
Code: [Select]

loop(){
  for (i=0; i<stepsPerRevolution; i++){
     myStepper.step(1);
     myStepper2.step(1);
}
  delay(500);
}


2. Search for the AccelStepper library, which is non-blocking
http://jsrintervalometers.blogspot.ca

Sajuuk

Thank you for your reply.
I will test this evening and I'll return with the results.

Best Regards,
A.

kf2qd

You could control the steppers directly - All you have to do is turn on the outputs in the correct sequence and the motor will step.

you could use something like this -

int StepCt1 = 0;
int Stepct2 = 0;

void Motor1(int Dir) {
   StepCt1 = StepCt1 + Dir;
   StepCt1 = StepCt1 && 3;
   select (StepCt1) {
      case 0:
          digitalWrite(8, HIGH);
          digitalWrite(9, LOW);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          break;
      case 1:
          digitalWrite(8, LOW);
          digitalWrite(9, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          break;
      case 2:
          digitalWrite(8, LOW);
          digitalWrite(9, LOW);
          digitalWrite(10, HIGH);
          digitalWrite(11, LOW);
          break;
      case 3:
          digitalWrite(8, LOW);
          digitalWrite(9, LOW);
          digitalWrite(10, LOW);
          digitalWrite(11, HIGH);
          break;
      }
}

Depending on your call to Motor1 the motor will step Clockwise, stay in place, or step counter clocwise (1, 0, -1)
Repeat the function as motor2 with the correct outputs and StepCt2.
The rate at which you call them controls the speed.

There are some ways of making the process a little faster, but this is a good place to start.

It can also be programmed to do half stepping or wave stepping with minor changes.

Sajuuk

After a little of tinkering, I decided to go with the AccelStepper. It is simpler to use...
Thank you for your replies :)

A.

ez8mm

having the same problem with the adafruit afmotor shield and the af library and the accel library and the my stepper library with the shield have not helped
EZ8MMPRO

kf2qd

It would appear that the libraries are designed for moving one motor at a time. You will probably have to write your own code to move 2 motors at the same time.

Go Up