I am new to Arduino and robotics. I am using two microstep drivers and two stepper motors, and based on the code, they are rotating at the same speed in the counterclockwise (CCW) direction. Could anyone help me with the changes I need to make in order to rotate the two motors with a phase shift of 90 degrees?
int x;
void setup()
{
pinMode(9, OUTPUT); // set Pin9 as PUL for motor 1
pinMode(8, OUTPUT); // set Pin8 as DIR for motor 1
pinMode(3, OUTPUT); // set Pin3 as PUL for motor 2
pinMode(2, OUTPUT); // set Pin2 as DIR for motor 2
}
void loop()
{
digitalWrite(8, HIGH);
digitalWrite(2, HIGH);
for (x = 0; x < 800; x++)
{
digitalWrite(9, HIGH);
delayMicroseconds(1000);
digitalWrite(9, LOW);
delayMicroseconds(1000);
digitalWrite(3, HIGH);
delayMicroseconds(1000);
digitalWrite(3, LOW);
}
delay(10);
}
It looks like you are pulsing one stepper HI/LO then the other stepper HI/LO. Why not pulse both HI, pause, then both LO. Also, I am guessing you have a 200 step/rev stepper in 1/4 step mode. That would mean 90 degrees would be StepPerRev/4 or 200. Make M2 wait for 200 steps before starting.
digitalWrite(9, HIGH);
if (x > 800/4)
digitalWrite(3, HIGH);
delayMicroseconds(1000);
digitalWrite(9, LOW);
if (x > 800/4)
digitalWrite(3, LOW);
delayMicroseconds(1000);