delays and interrupts?

i’m working on a relatively complicated program and i’m pushing up against the edge of what i understand. i’m working on an experimental arduino firmware for RepRap to control the speed of the extruder motor as an RPM (it basically needs to go a constant speed by sensing the actual rotational speed and compensating for any friction, etc.) here’s the setup:


  • a dc motor, controlled via PWM
  • a rotary encoder, read via quadrature encoding using pins 2/3 and an onchange interrupt
  • various stepper drivers, etc.


  • interrupt driven quadrature reading which sets a ‘position error variable’ for extruder motor. forward is positive.
  • timer1 interrupt to decrement the ‘position error variable’ at regular intervals (this is how we get the constant speed)
  • timer2 interrupt to measure the ‘position error variable’ at a regular interval and apply a PID algorithm to control the speed

anyway, it works great… when its just controlling the extruder speed (and nothing else is happening). however, as soon as i tell the printer to move the extruder head, it freaks out, and occasionally the firmware resets. after reading through the arduino library source last night, i think it may be a problem with the delayMicroseconds() function in the stepper movement function.

the way the stepper movement works is that it calculates a delay between steps, as well as the number of steps to take, then takes that number of steps with the appropriate delay between them. generally this delay is between 600 and 3000 usecs. the function uses the delayMicroseconds() function to wait. from reading through the function, it appears that it disables interrupts during the delay function.

so, i need a workaround. i need delays with microsecond precision, but i need them to be able to be interrupted. the interrupts should be pretty quick, so i’m not super worried about the speed.

can i do something like this: (and how much extra time would it use?)

for (i=0; i<delay_microseconds; i++)

or, should i write my own delayMicrosInterruptible() that doesnt disable the interrupts?

the code in question is all located here:

Your best bet might be to write your own delayMicroseconds() function that doesn’t disable interrupts. It won’t be as precise (since it will be interrupted), but I get the impression that catching the interrupts is more important than the exact stepper motor speed. Also, since the timer1 and timer2 interrupts sound pretty regular, you can probably compensate for them by scaling the number of microseconds you delay for. (The encoder interrupt is a bit trickier, since it’s not necessarily regular.)

thanks! i’ll give that a shot and report back with the results.

the great part about open source is that i can just copy the Arduino delayMicroseconds() and modify it to remove the interrupt disable.


Seems that a lot of people wind up doing this. Perhaps the arduino library should have a version of delayMicroseconds that doesn't block interrupts. It would be pretty sloppy for small delay values, but with a little hacking would work OK for larger delays.

My next project (I think) is going to require interruptible sub milli-second delays.