Delay without using delay() or millis() ?

Is it possible to have a delay , say 50ms, on Arduino Uno without using timer0 (that is, delay() or millis() )? I'm thinking a loop with NOPs (no-operation), how to implement that ?

Another question (unrelated to above): I'm using timer2 to generate interrupts every 250ms. I'm not using delay() or millis() anywhere in the sketch. Do I need to disable timer0 to avoid waking up from sleep every millisecond or will compiler or something realize to do that for me ? (I only want it to wake up every 250 ms)

Which sleep mode do you use (POWER_MODE_IDLE, SLEEP_MODE_IDLE,...)? Remember the documentation states

When using SLEEP_MODE_IDLE, care must be taken to ensure that the 8-bit timer is disabled if you're using the arduino layer. The timer can be disabled before entering sleep using the PRR = PRR | 0b00100000; statement and subsequently re-enabled once out of sleep using PRR = PRR & 0b00000000; . The PRR refers to the Power Reduction Register. One must note that if this timer is disabled, the millis(); cannot be relied upon anymore if you need reliable data comparison before and after a sleep command.

You can also explore Nick’s article on interrupts

Is it possible to have a delay , say 50ms, on Arduino Uno without using timer0 (that is, delay() or millis() )?
I’m thinking a loop with NOPs (no-operation), how to implement that ?

It would be easier to give a useful answer if you explain why you want to avoid using Timer0 for the normal Arduino timing.


Avr-libc has _delay_ms(); doesn’t use timers.

Hello generaly it is possible.

To implement delay you need to just do a meaningless calculation so as to waste cpu's cycles for instance a for loop .keep in find that you need to determine the cpu operating frequency for a uno that is 16 MHz which mean that it can do 16.000.000 simple operations in 1 second this way you can find how many iterations a for loop has to make in order to produce appropriate delay.

To answer to your second question,

as far as i know timers are by default shut down and you need to awake them by setting their control register and global interrupt register.So i dont think it will be a problem. Anyway you can shut it down easily by writting TIMSK0 = 0x00;

Generaly you can use all timers to generate interrupt (intterupt on compare mode or overflow) loading the appropriate values on them.

However if you are not planning to use millis anyway, you might as well consider using timer0 to implement interrupt at 250 ms .This will cause: millis to malfunction but you will not use it so no problem here to keep free timer2 which can be used for generating pwm and is also being used from other libraries,like servo

I use MsTimer2 to create a delay without using timer0. I use MsTimer2in the same way as millis. The Ultimate GPS project I did works much better using MsTimer2 over timer0

  //cause a GPS data read so that the
  //if (GPS.newNMEAreceived())
  //can do its thing;
  //this 1-ish ms count is used for timing of other routines
 MsTimer2::set(1, GPSSIGNAL);
  //enable interrupt
if (iLEDBlinkTicCount >= LED_BlinkInterval)

a few snippets of example