Pages: [1]   Go Down
Author Topic: Running two steppers at the same time  (Read 720 times)
0 Members and 1 Guest are viewing this topic.
Romania
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged


Romania
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Best Regards,
A.
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Romania
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

A.
Logged

NH USA
Offline Offline
Full Member
***
Karma: 0
Posts: 112
NEVER ENOUGH TIME TO BUILD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

EZ8MMPRO

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: