I am driving a set of four stepper motors with four Arduinos. Each stepper needs to run run at a different speed but should start and finish its motion at the same times. I am calculating the delay needed between each step as a sum of a millisecond delay and a microsecond delay. My problem is that sometimes the stepper motor "stutters", that is, misses a step. Seems like the code is non-deterministic and the Arduino gets busy doing something else. I am reading an encoder using interrupts at the same time, but the problem happens even if I omit the encoder. The problem is difficult to pin down. Sometimes the motion is perfectly smooth.
for(int i=0;i<numberSteps;i++){
digitalWrite(stepPin, HIGH);
delay(milliDelay); // millisecond delay (a long delay), for example 28 ms
delayMicroseconds(MSDelay); // microsecond delay (a short delay), for example, 320 us
digitalWrite(stepPin, LOW);
delay(stepDelay); // a quick delay between LOW and the next HIGH, 5 us
}
Thoughts?