All courses

Arduino C++ · Lesson 26 of 40

Time several tasks without blocking

Responsive Timing and State Machines

Wait by checking a clock, not stopping the sketch

With delay(), normal sketch execution waits before it can read another button or update a display. Interrupts can still run; the processor is not simply powered off. A responsive loop instead checks whether an interval has elapsed and returns quickly so other tasks get a turn.

millis() returns an unsigned long timestamp. Keep timestamps and intervals in unsigned long variables. On the classic Uno it wraps after about 49.7 days. Compare elapsed time with now − previous, using unsigned subtraction, rather than comparing now with previous + interval. Each independent task needs its own timestamp.

This works when the task is checked regularly and elapsed intervals do not span an entire counter cycle. A scheduler cannot detect multiple unobserved wraps.

Blink and report independently

Use the onboard LED; no wiring is required. Upload this complete sketch and open Serial Monitor at 9600 baud.

Arduino C++
const unsigned long blinkEvery = 250;
const unsigned long reportEvery = 1000;
unsigned long lastBlink = 0;
unsigned long lastReport = 0;
bool ledOn = false;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  const unsigned long now = millis();
  if (now - lastBlink >= blinkEvery) {
    lastBlink = now;
    ledOn = !ledOn;
    digitalWrite(LED_BUILTIN, ledOn);
  }
  if (now - lastReport >= reportEvery) {
    lastReport = now;
    Serial.println(F("Still responsive"));
  }
}

lastBlink = now schedules the next toggle relative to this actual execution time, which can drift. lastBlink += blinkEvery preserves scheduled cadence but needs a deliberate missed-deadline policy. Neither creates parallel execution: each block must still finish quickly.

Expected result: The LED toggles four times a second and a message appears roughly once a second. Change reportEvery without changing blinkEvery. If both rates change, you have accidentally coupled the clocks.

Exercise: Add a second message every 1700 ms with its own timestamp. Explain why no common delay can express these independent intervals cleanly.

Reason through a wrap

Suppose previous is 4294967290 and now is 4 on a 32-bit unsigned counter. Unsigned subtraction produces 10, so an interval of 10 is due. A signed int timestamp would lose information long before this point on an Uno R3.

For a software-only test, extract a helper bool due(unsigned long now, unsigned long before, unsigned long interval) returning now - before >= interval. Try 100/90/10, 99/90/10, and 4/4294967290UL/10. Expected answers are true, false, true. Supply simulated values; you do not need to leave a board running for seven weeks.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.