# Delay function in Arduino microcontroller

Arduino IDE has a "Blink without Delay" example code. In here it comments:

"Turns on and off a light emitting diode(LED) connected to a digital pin, without using the delay() function. This means that other code can run at the same time without being interrupted by the LED code."

Could you give me some insight what is meant here?

The delay() function does not return until the requsted interval has expired. During that time it constantly reads the clock, watching for the end of the interval. This means that your program can't do anything else during that time.

An alternative is to for your program set a timer that will interrupt whatever the program is doing when the time expires by calling a function to (in this case) toggle the LED state. Meanwhile, once the timer is set, your program can continue on to do something more useful.

In a blinky program, where blinking an LED is the whole point, there isn't anything else needing doing, but in general, you'll want your programs to be able to maintain a display, react to buttons or a keyboard, and read a few sensors, for example, and appear to do them simultaneously.

Update:

A clock or timer is a hardware register the program can read, which is automatically counted up or down at a fixed rate, such as once every millisecond. This counting is done by the hardware without the program having to do anything. To delay for say, 2 seconds, the delay() function will read the clock register, add 2000 to it (the number of milliseconds of the required delay) to find the ending time, then keep reading the clock register until it equals or exceeds that time.

In the second case, above, a timer register would be set to 2000 (again, the number of milliseconds to delay), but in this case the hardware decrements that register every millisecond, and will interrupt the program when the timer register gets to 0.

• You wrote "During that time it constantly reads the clock". I don't know about clocks and timers how they function by code. Apr 2, 2014 at 20:23

First, look at the contents of the Blink sketch. As JRobert says, delay() doesn't return until the specified interval (here, 1000 milliseconds, or 1 second) has expired. That means your loop() function can't do anything else while it's waiting for that one second to pass (it's waiting for delay() to return). To use an analogy, imagine if you turn on the LED, stare at your watch until a second passes, turn off the LED, and stare at your watch for another second (and repeat).

Now, look at BlinkWithoutDelay. This program repeatedly compares the current time, currentMillis, with a previously recorded time, previousMillis. If the difference exceeds 1 second (currentMillis - previousMillis >= interval), it updates the recorded time (previousMillis = currentMillis;) and toggles the state of the LED. Otherwise, it starts over and checks the time again.

(The current time is obtained with the function millis(), which returns the time in milliseconds that has elapsed since the Arduino began running the program. This value is incremented at the correct rate through one of the timers in the Arduino's microcontroller, although the BlinkWithoutDelay sketch doesn't work with the timer directly.)

The big difference from Blink is that in BlinkWithoutDelay, you can easily add more code to the loop() function to run between time checks (a good place is at the very beginning, where you see the comment // here is where you'd put code that needs to be running all the time.) This would be kind of like repeatedly glancing at your watch to check if it's been one second, and toggling the LED if it has; you're free to do other tasks between checking your watch once and checking it again. One thing to be careful of is that those other tasks don't take too long; otherwise, you/the Arduino might spend more than one second on those tasks before checking the time again.