Loading...
Poll
Question: 2 stepper motors running at SAME TIME - Arduino uno
2 stepper - 2 (40%)
Arduino uno - 3 (60%)
Total Voters: 5

Pages: [1]   Go Down
Author Topic: 2 stepper motors running at SAME TIME - Arduino uno  (Read 3839 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Luv Life Live Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,
Im trying to get two stepper motors to rotate at the same time. Im using two Sn754410NE drivers and an Uno Arduino. pins 2-5 go to stepper 1 and pins 8-11 goes to stepper 2. when i run the program (shown below) both steppers rotate but NOT AT THE SAME TIME:(. the first one rotates and then the second one rotates. is there a way to mod this program so BOTH STEPPERS RUN AT THE SAME TIME:). ANY HELP would be appreciated. thanks in advance. this is the code that im using (its basically from the samples, just modifies a bit):
CHEERS!!

#include <Stepper.h>

const int step_360 = 200;// 360 number of steps per/rev                              
// initialize the stepper library on pins 2-5 n 8-11
Stepper myStepper1(step_360,2,3,4,5);
Stepper myStepper2(step_360,8,9,10,11);      

void setup()
{
  // set the speed at 60 rpm:
  myStepper1.setSpeed(60);//left
  myStepper2.setSpeed(60);//right
  // initialize the serial port:
  Serial.begin(9600);
}
void loop()
{
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper1.step(step_360);
  myStepper2.step(step_360);
  delay(500);    
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 314
Posts: 35507
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when i run the program (shown below) both steppers rotate but NOT AT THE SAME TIME
No need to shout. Of course they don't run at the same time. The step() method is a blocking function. It does not return until the motor has completed stepping the specified number of steps. The speed and number of steps defines how long that takes.

You can't really make them step at the same time. What you can do, though, is to make them step one at a time in much smaller steps, so that they appear to be moving at the same time.

Code:
for(int s=0; s<step_360; s++)
{
  myStepper1.step(1);
  myStepper2.step(1);
}

Depending on the driver and the stepper motor, you may be able to use half, quarter, or eighth step mode, and step in even smaller amounts, making the motors move more and more in sync,
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 429
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can in either of two ways: Firstly, you can connect one set of stepper control signals from the Arduino to both driver channels. This has the effect of running the steppers in lockstep. (I once built a CNC router with two steppers driving the x-axis, and this was how I ensured that the steppers stayed in sync.)

If that's not what you want, you can make sure that both sets of control signals are connected to pins on the same port, and then exercise control by doing 8-bit wide outputs to the port rather than to individual pins with digitalWrite().

Edit: clarification
« Last Edit: February 25, 2012, 08:39:17 am by Morris Dovey » Logged

There's always a better way!

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Luv Life Live Life
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when i run the program (shown below) both steppers rotate but NOT AT THE SAME TIME
No need to shout. Of course they don't run at the same time. The step() method is a blocking function. It does not return until the motor has completed stepping the specified number of steps. The speed and number of steps defines how long that takes.

You can't really make them step at the same time. What you can do, though, is to make them step one at a time in much smaller steps, so that they appear to be moving at the same time.

Code:
for(int s=0; s<step_360; s++)
{
  myStepper1.step(1);
  myStepper2.step(1);
}

Depending on the driver and the stepper motor, you may be able to use half, quarter, or eighth step mode, and step in even smaller amounts, making the motors move more and more in sync,

thanks sooo much!!! that for loop seems to do the trick. since u can tell my programing skills are next to nil, how would u modify the loop so one stepper does say 90 degrees and one does 360.? thanks again. greatly appreciated!:)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 314
Posts: 35507
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how would u modify the loop so one stepper does say 90 degrees and one does 360.?
Determine which position change is larger (360). Then, define the amount each motor needs to step on each pass through loop.
float step1Inc =  90.0/360.0;
float step2Inc = 360.0/360.0;

Then, loop for 360 steps. On each pass through loop, set the new position.
Code:
float step1Pos = 0.0;
float step2Pos = 0.0;

for(int i=0; i<360; i++)
{
  step1Pos += step1Inc;
  step2Pos += step2Inc;

  // Move stepper 1 to step1Pos
  // Move stepper 2 to step2Pos
}
Since the stepper expects to move in whole steps, step1Pos = 0.25, 0.50, and 0.75 will cause nothing to happen for stepper 1, while stepper 2 steps 3 times. Then when step1Pos gets to 1.0, it will step once.
Logged

South Texas
Offline Offline
God Member
*****
Karma: 8
Posts: 976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try something like this - Speed might be an issue -

Dir1 and Dir2 control the direction the motor will step +1 and -1. Dir1 or Dir2 = 0 will result in no motion for that motor.
How often you call them controls the motor speed. Each time you call motor 1 or Motor 2 you will get 1 step.
You could use half stepping but that would require 8 patterns (0-7) and the necesary changes to Motor1 and Motor2


Code:
int StepCt1 = 0;
int StepCt2 = 0;
int Dir1;
int Dir2;


void Motor1(int Direction){
   StepCt1=Stepct1+Direction;
   StepCt1=StepCt1 && 3;       / StpepCt1 ranges from 0 to 3,  -1 = B11111111 so B11111111 && B00000011 = B00000011
   switch (StepCt1)
      case 0:
          digitalWrite(2, HIGH);
          digitalWrite(3, LOW);
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
          break;
      case 1:
          digitalWrite(2, LOW);
          digitalWrite(3, HIGH);
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
          break;
      case 2:
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, HIGH);
          digitalWrite(5, LOW);
          break;
      case 3:
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, LOW);
          digitalWrite(5, HIGH);
          break;
    }
}
void Motor2(int Direction){
   StepCt2=Stepct2+Direction;
   StepCt2=StepCt2 && 3;       / StpepCt1 ranges from 0 to 3,  -1 = B11111111 so B11111111 && B00000011 = B00000011
   switch (StepCt2)
      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;
    }
}
Logged

Pages: [1]   Go Up
Print
 
Jump to: