Running 2 stepper motors simultaniously at different speeds

Ok, so the stepper motor controllers I have need code like this to run:

digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(1000);

digitalWrite(STEP_PIN, LOW);

delayMicroseconds(DELAY);

the way to control the speed the motors run at is to control the DELAY value, thus controlling how long they are "off" for in between steps.

the issue arises when trying to run more than one motor. It essentially becomes a duty cycle problem, except the high state must be for 1000 microseconds, so the duty cycle is non-uniform in frequency between the two motors. To be clear, when I say duty cycle I'm not talking about PWM, that has much too fast of a pulse rate for these motors to run off of it.

If I do this:

digitalWrite(STEP_PIN1, HIGH);
digitalWrite(STEP_PIN2, HIGH);
delayMicroseconds(1000);

digitalWrite(STEP_PIN1, LOW);
delayMicroseconds(DELAY1);
digitalWrite(STEP_PIN1, HIGH);
delayMicroseconds(DELAY2 - DELAY1);
digitalWrite(STEP_PIN2, LOW);
delayMicroseconds(1000 + DELAY1 - DELAY2);
digitalWrite(STEP_PIN1, HIGH);
etc.

then it become massively out of sync within just a few repetitions, and if DELAY2 is > 1000 then it is out of sync on the first repetition.

How can this be done?

Hi

First of all delay() is what we call a blocking function. Meaning that while you call delay(1000) for example , nothing gets executed for that amount of time. This makes delay() a function wich is not suited for applications where you want to do multiple things at once. Basically if you have 2 motors and you use delay() , one waits for the other.

You could look into "blink without delay" examples, the internet is full of them.

You might also want to look into libraries like AccelStepper or DMstepper , depending on your application, they may well do the job for you

I thought asking the arduino to do if statements that frequently would ruin the accuracy of the pulse, but actually that solution worked quite well, thank you.

You might want to look at the sketch I used to control two steppers of PT camera system with jitter removal:
https://forum.arduino.cc/index.php?topic=647703.msg4633850#msg4633850

While I used same speed for both, nothing prevents you from using different speeds for the two steppers.