I have made a simple traffic light sketch, and I wanted to test interrupts. I should mention that this is using the arduino as ISP programming an ATTiny85. I have included an interrupt sub-routine, however, when the ISR runs I only get about half the delay time (5 seconds instead of 10 seconds). I don't understand why and was wondering if I am missing something.
Aside from the fact that it simple won't work right, using delay() in an interrupt handler completely defeats the purpose of an interrupt. Interrupt handlers should do as little as possible, then return. You should never do time-consuming operations, most especially time-wasting operations like delay(), in an interrupt handler.
Actually, I just threw the delay in there very lazily to enable me to see the interrupt working, as I have never used an interrupt before. Then I was confused as to why the timing was wrong.
After a little bit of experimentation I realized that interrupts don't seem to handle millis() either (obviously taking up too much time as well) .
So my question is;
Can I use interrupts to run a segment of code that will take some time to perform?....in this case, I want to use interrupts to stop the normal sequential program and switch everything off for a 10 seconds.
delay() will hang indefinitely if called in an ISR because it waits for other interrupts
to happen, but they cannot because interrupts are already disabled due to being inside
the first ISR...
For the same reason you cannot call Serial methods.
Basically if your ISR takes longer than 100us, think again...