Yes, thanks UKHellibob, I was trying to type the essence of the code for illustrative purposes. Mistyped 1000 when I meant 2000. Anyway, the problem is that the delay becomes much shorter. delay(2000) is only a 30 millisecond delay according to the oscilloscope.
Thanks, robtillaart. I have taken a look at it. It will definitely make my code more complex if I don't use delay, but there is the advantage of it being more responsive to to input. Nevertheless, I am still curious why setting up a timer interrupt (even without first calling the cli() function), would affect the delay. Any ideas?
Post the code that is showing this behavior. The code you posted turns off the interrupts and then it's clear that a call to delay() will never return.
My guess is that you have code in the setup routine that is changing the Timer0 registers which is responsible for the correct function of millis(), delay(), etc.
It seems like most people know the prerequisites for delay. The prerequisites aren't mentioned in the reference page.
I don't think that "the prerequisites for delay" are what you need to understand. It is how to use interrupts, and what, exactly, an ISR should (and should NOT) do that you need to understand.
Interrupts and interrupt servicing are not beginner topics. It is expected that, by the time you know you need to use interrupts, that you will have understood what an ISR should (and should not) do.
yangres2002:
As far as I can tell from the oscilloscope, the following code does not toggle pin 8 every 200ms 8 times every 2 seconds.The toggle is every 4ms.
Thanks to everyone for their contributions. I am going to use the millis() function instead of delay. The discussion from here on in is strictly academic. The problem I had to begin with stemmed from not knowing the prerequisites of the delay function. I can take my time and read all about interrupts in the atmel manual for the chip, but there seems to be no detailed reference of the delay function on how it is implemented.
but there seems to be no detailed reference of the delay function on how it is implemented.
This sounds a bit like: "Nobody told me and the manual doesn't specify that the breaks of my car do not work as intended on the frozen lake."
If you would have read the page of the reference regarding interrupt service routines (attachInterrupt() - Arduino Reference) there is a note that delay() and other stuff don't work correctly inside interrupt handlers. Don't blame the Arduino documenters for you not reading the necessary documentation but using stuff (ISR) not specified in the reference.
there seems to be no detailed reference of the delay function on how it is implemented
Actually there is and you already have it. It's the source code for your IDE, which has been brought out on the forum, dissected and IIRC improved versions posted.
There is no more accurate comment than the code itself.