Skip to Content
Circuitera

Run two Arduino timers without blocking loop()

Compare two periodic jobs, then run an Uno sketch that flashes the onboard LED and writes a serial heartbeat on separate schedules.

0 of 3 complete · Progress saved on this device

Compare two schedules

The onboard LED changes state every 250 ms. A serial heartbeat is due every 1,000 ms. At 1,000 ms both jobs can run in the same pass. Their deadlines are independent, and each has its own most recent execution time.

LED and serial schedule
4 LED toggles · 1 heartbeats
How many LED toggles are due by 1,000 ms, including the toggle at 1,000 ms?

Keep elapsed time separate

Read millis() once at the top of loop() and subtract each job’s own timestamp. Unsigned subtraction handles wraparound when the elapsed interval is shorter than the counter’s full cycle. Assigning now to the timestamp after a slow iteration skips missed work instead of bursting.

Fill in the saved timestamp for the heartbeat job.

Check which job stopped

Run and watch both the board LED and simulated serial messages at 9600 baud. Change only the LED interval. If both rates change, check for a shared timestamp or a blocking call in the loop. Long operations can still make deadlines late because this is one cooperative loop.

Open the exact Uno example

unsigned long lastBlink = 0, lastReport = 0;
bool lit = false;

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

void loop() {
  const unsigned long now = millis();
  if (now - lastBlink >= 250UL) {
    lastBlink = now;
    lit = !lit;
    digitalWrite(LED_BUILTIN, lit);
  }
  if (now - lastReport >= 1000UL) {
    lastReport = now;
    Serial.println("heartbeat");
  }
}

Verified with [email protected] on Uno R3 (2026-12-05 UTC). The matching circuit passed its Uno model checks. This check did not use physical hardware.

Continue learning

Sources