Bird
Raised Fist0
Arduinoprogramming~7 mins

Multiple timed events with millis() in Arduino

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Using millis() lets you run many timed tasks without stopping your program. It helps your Arduino do several things at once smoothly.

You want to blink two or more LEDs at different speeds without delay.
You need to read sensors and update a display at different times.
You want to make a game where multiple actions happen on their own schedule.
You want to avoid using <code>delay()</code> so your program stays responsive.
Syntax
Arduino
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 1000; // 1 second
const long interval2 = 500;  // 0.5 second

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis1 >= interval1) {
    previousMillis1 = currentMillis;
    // code for first timed event
  }

  if (currentMillis - previousMillis2 >= interval2) {
    previousMillis2 = currentMillis;
    // code for second timed event
  }
}

Use unsigned long to store time values from millis().

Always subtract previous time from current time to handle timer overflow safely.

Examples
Single timed event example toggling an LED every second.
Arduino
unsigned long previousMillis = 0;
const long interval = 1000;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    // toggle LED
  }
}
Two timed events running independently with different intervals.
Arduino
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 1000;
const long interval2 = 2000;

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis1 >= interval1) {
    previousMillis1 = currentMillis;
    // toggle LED1
  }

  if (currentMillis - previousMillis2 >= interval2) {
    previousMillis2 = currentMillis;
    // toggle LED2
  }
}
Shows how subtraction handles overflow safely.
Arduino
// Edge case: when millis() overflows (after ~50 days), subtraction still works correctly
unsigned long previousMillis = 4294967290;
const long interval = 20;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    // safe even if millis() overflowed
  }
}
Sample Program

This program blinks two LEDs at different speeds using millis(). It prints the LED states to the Serial Monitor so you can see the timing.

Arduino
#define LED1 13
#define LED2 12

unsigned long previousMillisLED1 = 0;
unsigned long previousMillisLED2 = 0;

const long intervalLED1 = 1000; // 1 second
const long intervalLED2 = 500;  // 0.5 second

bool led1State = LOW;
bool led2State = LOW;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  Serial.begin(9600);
  Serial.println("Starting multiple timed events demo");
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillisLED1 >= intervalLED1) {
    previousMillisLED1 = currentMillis;
    led1State = !led1State;
    digitalWrite(LED1, led1State);
    Serial.print("LED1 toggled to: ");
    Serial.println(led1State ? "ON" : "OFF");
  }

  if (currentMillis - previousMillisLED2 >= intervalLED2) {
    previousMillisLED2 = currentMillis;
    led2State = !led2State;
    digitalWrite(LED2, led2State);
    Serial.print("LED2 toggled to: ");
    Serial.println(led2State ? "ON" : "OFF");
  }
}
OutputSuccess
Important Notes

Time complexity: O(1) per timed event check.

Space complexity: O(1) for each timer variable.

Common mistake: Using delay() instead of millis() blocks the program and stops other events.

Use this method when you want your Arduino to do many things at once without waiting.

Summary

Use millis() to track time without stopping your program.

Store previous times in unsigned long variables and compare with current millis().

Subtract times to handle timer overflow safely and run multiple timed events independently.

Practice

(1/5)
1. What is the main advantage of using millis() for timing multiple events in Arduino instead of delay()?
easy
A. It resets the Arduino automatically.
B. It stops the program until the time passes.
C. It makes the program run slower.
D. It allows the program to run other tasks while waiting.

Solution

  1. Step 1: Understand delay() behavior

    delay() pauses the whole program, stopping all other actions.
  2. Step 2: Understand millis() advantage

    millis() returns elapsed time without stopping the program, so other tasks can run simultaneously.
  3. Final Answer:

    It allows the program to run other tasks while waiting. -> Option D
  4. Quick Check:

    millis() enables multitasking [OK]
Hint: Remember: delay() stops, millis() doesn't [OK]
Common Mistakes:
  • Thinking millis() pauses the program
  • Confusing delay() with non-blocking timing
  • Believing millis() resets Arduino
2. Which of the following is the correct way to declare a variable to store the last time an event occurred using millis()?
easy
A. unsigned long lastEventTime = 0;
B. int lastEventTime = 0;
C. float lastEventTime = 0.0;
D. char lastEventTime = '0';

Solution

  1. Step 1: Identify the correct data type for time

    Since millis() returns an unsigned long value, the variable must be unsigned long.
  2. Step 2: Check variable initialization

    Initializing to 0 is correct to mark the start time.
  3. Final Answer:

    unsigned long lastEventTime = 0; -> Option A
  4. Quick Check:

    Use unsigned long for millis() times [OK]
Hint: Use unsigned long for time variables with millis() [OK]
Common Mistakes:
  • Using int which can overflow quickly
  • Using float which is not precise for time
  • Using char which is for characters, not numbers
3. What will be the output of the following Arduino code snippet if millis() returns 5000 at the moment of checking?
unsigned long previousMillis = 3000;
unsigned long interval = 1500;

if (millis() - previousMillis >= interval) {
  Serial.println("Event triggered");
} else {
  Serial.println("Waiting");
}
medium
A. Waiting
B. No output
C. Event triggered
D. Compilation error

Solution

  1. Step 1: Calculate elapsed time

    Elapsed time = 5000 (current millis) - 3000 (previousMillis) = 2000 ms.
  2. Step 2: Compare elapsed time with interval

    Interval is 1500 ms. Since 2000 >= 1500, the condition is true, so "Event triggered" should print.
  3. Final Answer:

    Event triggered -> Option C
  4. Quick Check:

    Elapsed time 2000 >= 1500 triggers event [OK]
Hint: Subtract previousMillis from millis() to check elapsed time [OK]
Common Mistakes:
  • Mixing up >= and > operators
  • Forgetting to subtract previousMillis
  • Assuming output without calculation
4. Identify the error in this code snippet that tries to blink two LEDs at different intervals using millis():
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 1000;
const long interval2 = 2000;

void loop() {
  if (millis() - previousMillis1 >= interval1) {
    digitalWrite(LED1, !digitalRead(LED1));
    previousMillis1 = millis();
  }
  if (millis() - previousMillis1 >= interval2) {
    digitalWrite(LED2, !digitalRead(LED2));
    previousMillis2 = millis();
  }
}
medium
A. LED pins are not defined.
B. Second if condition uses previousMillis1 instead of previousMillis2.
C. Intervals should be unsigned long, not long.
D. digitalWrite cannot toggle LEDs.

Solution

  1. Step 1: Check timing variables in conditions

    The second if condition incorrectly uses previousMillis1 instead of previousMillis2, causing wrong timing for LED2.
  2. Step 2: Understand impact of error

    This mistake means LED2 timing depends on LED1 timing, so LED2 won't blink at its own interval.
  3. Final Answer:

    Second if condition uses previousMillis1 instead of previousMillis2. -> Option B
  4. Quick Check:

    Each event needs its own previousMillis variable [OK]
Hint: Use separate previousMillis for each timed event [OK]
Common Mistakes:
  • Reusing the same previousMillis variable for multiple events
  • Not updating previousMillis after event
  • Confusing interval variables
5. You want to control three LEDs blinking at 500ms, 1000ms, and 1500ms intervals respectively without using delay(). Which approach correctly manages all three timed events using millis()?
hard
A. Use three separate previousMillis variables and check each with its own interval inside loop().
B. Use one previousMillis variable and reset it after each LED toggles.
C. Use delay(500) and toggle all LEDs together.
D. Use millis() only once and toggle LEDs based on dividing millis() by intervals.

Solution

  1. Step 1: Understand independent timing needs

    Each LED needs its own timer to blink independently at different intervals.
  2. Step 2: Evaluate options

    Use three separate previousMillis variables and check each with its own interval inside loop(), allowing independent timing without blocking.
  3. Final Answer:

    Use three separate previousMillis variables and check each with its own interval inside loop(). -> Option A
  4. Quick Check:

    Separate timers for each event [OK]
Hint: Assign each event its own timer variable for independent control [OK]
Common Mistakes:
  • Using one timer for all events causing sync issues
  • Using delay which blocks other events
  • Trying to calculate toggles from one millis() value without state