Sub topic 7
Blinking Without `delay()`
Prerequisites and the problem with `delay()`
This chapter assumes you know the basic Arduino sketch structure, setup() and loop(), and can use pinMode(), digitalWrite(), if statements, Boolean variables, and integer types.
A simple blink often uses delay():
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}This works, but delay(500) stops the sketch for 500 milliseconds. During that time, loop() does not read a button, update a sensor, or perform any other task. A longer delay makes the sketch feel unresponsive.
A non-blocking design checks the time on every pass through loop(). It changes the LED only when enough time has passed.
Measuring elapsed time with `millis()`
millis() returns the number of milliseconds since the Arduino started running the current sketch. It does not wait. The function returns immediately, so the rest of loop() can continue.
The usual pattern stores the time of the previous action:
const unsigned long INTERVAL_MS = 500;
unsigned long previousMillis = 0;
bool ledState = LOW;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
unsigned long now = millis();
if (now - previousMillis >= INTERVAL_MS) {
previousMillis = now;
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
}
// Other work can run here on every pass through loop().
}INTERVAL_MS is the required interval. previousMillis records when the LED last changed. ledState records whether the LED should be on or off.
The subtraction calculates elapsed time:
nowis the current timestamp.previousMillisis the timestamp of the last change.now - previousMillisis the time since that change.
The LED changes every 500 milliseconds. Because loop() keeps running, other code can run between LED changes.
Why unsigned subtraction matters
On many Arduino boards, millis() uses an unsigned long. Its counter eventually reaches its maximum value and wraps back to zero. This is called rollover.
Write elapsed-time checks like this:
if (millis() - startTime >= timeout) {
// timeout has elapsed
}Unsigned subtraction continues to produce the correct elapsed duration across rollover, as long as the interval is shorter than the counter's full range.
Avoid comparing against a precomputed deadline:
// Fragile across rollover:
if (millis() >= startTime + timeout) {
// timeout has elapsed
}The addition can wrap before the comparison, causing the deadline test to fail.
> callout note > Use unsigned time variables and compare elapsed time with current - previous >= interval. This pattern is safer than comparing millis() with a calculated future deadline.
Choosing an update style
Assigning previousMillis = now measures each interval from the moment the action actually happens. This is easy to understand and prevents a delayed loop from immediately performing many catch-up actions.
For tasks that must maintain a precise schedule, code can instead add the interval:
previousMillis += INTERVAL_MS;That preserves the original cadence, but a loop that falls far behind may need several catch-up actions. Choose the version based on the task.
Exercise
Why can the blinking example respond to a button while the LED is waiting for its next change?
Show answer
It never waits with delay(). It checks millis() quickly, then returns through loop(), leaving time for button-reading code and other tasks.