attachInterrupt() and delay()?

Simple question. In my code running on an Arduino Uno I attach interrupts in setup() to digital pins 2 and 3 to sense the output of a rotary encoder with some very simple interrupt service routines. In the main loop() I have some delay() calls that are several seconds long. This will be changed eventually, but I thought I'd leave it this way temporarily to see how interrupts and delays interact.

I wanted to see if anyone knows for sure whether the interrupts are sensed and serviced during the delay() calls? The reason I ask, is that it appears that they are serviced- I've had the value of the rotary encoder printed before and after the delays, and when I turn the encoder during the delay periods, I can see that the value of the encoder is incremented what appears to be the correct amount according to how much I've turned it.

Can someone confirm whether this is just luck, or if the interrupts are serviced as normal during delays? I've got very repeatable results doing some hand testing and I almost can't believe it. Thanks!

Interrupts are detected and ISRs run during a delay(). A delay() is just a piece of code that runs round in a circle for a period of time.

It is best not to use the delay() function except for quick and dirty tests. Use millis() to manage timing without blocking as illustrated in Several Things at a Time


This is why interrupts are useful. Your code might be doing something else but it's vital that it catches every transition on the rotary encoder. So use an interrupt.

Delays are generally bad for medium-to-large projects but they are useful and an interrupt-based encoder library doesn't care.