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);
}*/
}
//}