Rotate two stepper motor with phase shift

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);

}

On program start move one motor to 0° and the other to 90°. With the same step commands for both motors they will stay shifted by those 90°.

For a time shift a base frequency or repetition time is required.

Thank you...I have looking to make a time shift..

shouldn't the motors start with a 90 deg offset which means the should each be stepped at the same time?

if the don't start with a 90 deg offset, shouldn't one be stepped to the 90 deg position and then both can be stepped together

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);

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