How to run two stepper motors simultaneoulsly using cnc sheild, fixed to arduino

I am planning to run two stepper motors simultaneously using a cnc shield with A4988 drivers mounted and cnc shield is mounted to Arduino Uno. I am able to run them consecutively i.e one after another but not able to run both at a time.
Below attached code is for four stepper motors running one after another. But I need only two-steppers run at a time. Thanks
#define enable 8

#define xStep 2 //x Axis Step Pin
#define yStep 3 //y Axis Step Pin
#define zStep 4 //z Axis Step Pin
#define xDir 5 //x Axis Direction Pin
#define yDir 6 //y Axis Direction Pin
#define zDir 7 //z Axis Direction Pin
#define aStep 12 //A Axis Step Pin
#define aDir 13 //A Axis Direction Pin

int steps = 1000;
int stepDelay = 1500;
void step(boolean dir, byte dirPin, byte stepperPin, int steps)
{
digitalWrite(dirPin, dir);
delay(100);
for (int i =0; i< steps; i++)
{
digitalWrite(stepperPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepperPin, LOW);
delayMicroseconds(stepDelay);

}
}
void setup() {
// put your setup code here, to run once:
pinMode(xDir, OUTPUT);
pinMode(xStep, OUTPUT);
pinMode(yDir, OUTPUT);
pinMode(yStep, OUTPUT);
pinMode(zDir, OUTPUT);
pinMode(zStep, OUTPUT);
pinMode(aDir, OUTPUT);
pinMode(aStep, OUTPUT);
pinMode(enable, LOW);

}

void loop() {
//int k=0;
// put your main code here, to run repeatedly:

step(false,xDir,xStep,800); //X Axis Rotate Clockwise
step(false,yDir,yStep,800); //Y Axis Rotate Clockwise
step(false,zDir,zStep,800); //Z Axis Rotate Clockwise
step(false,aDir,aStep,800); //A Axis Rotate Clockwise
//delay(2000);
step(true,xDir,xStep,800); //X Axis Rotate Anti Clockwise
step(true,yDir,yStep,800); //Y Axis Rotate Anti Clockwise
step(true,zDir,zStep,800); //Z Axis Rotate Anti Clockwise
step(true,aDir,aStep,800); //A Axis Rotate Clockwise
//delay(2000);
/* for(int k=0;k<2;k++)
{
if (k=0)
{ step(false,xDir,xStep,800); //X Axis Rotate Clockwise
delay(2000);
step(true,xDir,xStep,800); //X Axis Rotate Anti Clockwise
delay(2000);
}
else if(k=1)
{
step(false,yDir,yStep,800); //Y Axis Rotate Clockwise
delay(2000);
step(true,yDir,yStep,800); //Y Axis Rotate Anti Clockwise
delay(2000);
}*/
}
//}

To run them simultaneously, you will need to step them one step at a time and have other code in loop that looks after the total number of steps to perform. As you have it now, your step routine is being asked to do 800 steps before the other motor gets its turn.

Even if you do single stepping, you'll still have one motor moving before the other. If that matters, you may need to look into port manipulation so you can step multiple motors at the exact same time.

Yeah, I have tried single stepping and it's not favourable for us. But how do we do this port manipulation with cnc shield mounted on Arduino.
Thanks

dkpuvvula:
Yeah, I have tried single stepping and it's not favourable for us.

If the code you posted is satisfactory apart from the lack if simultaneity then I suspect the problem is with the way you did the single stepping. I have a personal program that does single stepping for 4 stepper motors.

Please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. That way we can focus on the parts you need help with rather than wasting time on things that you can do.

Another key factor is how the motion of the two motors is related. There is a big difference between both motors starting at the same time, moving different numbers of steps and finishing at different times and where (like on a 3D printer) they both need to start and stop at the same time even though they move different numbers of steps.

...R

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