Sub topic 4
The Cost of `delay()`
Assume you know the basic Arduino sketch structure and can use pinMode(), digitalRead(), and digitalWrite(). This chapter focuses on what happens while delay() is running and why that matters when a sketch must respond to more than one thing.
What `delay()` actually does
delay(milliseconds) pauses the execution of the current sketch for the requested time. During that pause, Arduino does not return to loop() to run your code again.
Consider this sketch:
const uint8_t BUTTON_PIN = 2;
const uint8_t LED_PIN = LED_BUILTIN;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
}
}The LED stays on for one second, then off for one second. However, the button is checked only after both delays finish. A press that happens during either delay is not acted on until as much as two seconds later. If the button is pressed and released entirely during that period, the sketch may never observe it.
The processor is not necessarily electrically idle. Arduino’s timing system can continue counting, and some hardware peripherals may continue operating. The important point is that your loop() code is not running during the delay.
Blocking versus responsive code
A blocking operation prevents other work from progressing until it completes. In the example, the LED timing blocks button handling.
This creates problems when a sketch needs to:
- Read buttons or sensors regularly.
- Update a display.
- Communicate over serial, I2C, or SPI.
- Control several outputs with different periods.
- React quickly to safety or user input.
The following timeline shows why a button press can be missed:
The sketch also cannot easily give two activities different schedules. For example, a one-second LED blink combined with a sensor check every 20 milliseconds would be tied to the delays unless the code repeatedly interrupted its own waiting.
Choosing a suitable response time
The required response time depends on the application. A status LED may tolerate a delay of hundreds of milliseconds. A user interface usually feels better when input is checked within a few tens of milliseconds. A safety-related input may need an even faster and more carefully designed response.
| Project behavior | Effect of a long `delay()` |
|---|---|
| Simple demonstration blink | Usually acceptable |
| Button-controlled menu | Input feels late or is missed |
| Serial communication | Data may not be processed promptly |
| Motor or safety control | Response can be unsafe |
The usual alternative is to record when an activity last occurred, check the current time on every pass through loop(), and perform the activity only when its interval has elapsed. Arduino’s millis() function provides that elapsed-time information. That approach allows button handling and other tasks to run between scheduled actions.
Exercise
Exercise
A sketch calls delay(5000) before reading a button. What is the longest time the sketch might wait before responding to a press, and why could a short press be missed?
Show answer
It might wait five seconds because the button is not read during the delay. A press that begins and ends during those five seconds may never be observed.