Bird
Raised Fist0
Arduinoprogramming~5 mins

Multiple timed events with millis() in Arduino - Time & Space Complexity

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
Time Complexity: Multiple timed events with millis()
O(n)
Understanding Time Complexity

When using multiple timed events with millis(), it's important to know how the program's work grows as more events are added.

We want to understand how checking many timers affects the program's speed.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const unsigned long interval1 = 1000;
const unsigned long interval2 = 2000;

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

  if (currentMillis - previousMillis1 >= interval1) {
    previousMillis1 = currentMillis;
    // Event 1 action
  }

  if (currentMillis - previousMillis2 >= interval2) {
    previousMillis2 = currentMillis;
    // Event 2 action
  }
}
    

This code checks two timed events repeatedly to see if their intervals have passed, then triggers actions.

Identify Repeating Operations
  • Primary operation: Two if-checks inside the loop function.
  • How many times: Both checks run every time loop() runs, which is very often.
How Execution Grows With Input

Each additional timed event adds one more if-check inside the loop.

Number of Events (n)Approx. Operations per loop
22 if-checks
1010 if-checks
100100 if-checks

Pattern observation: The number of checks grows directly with the number of timed events.

Final Time Complexity

Time Complexity: O(n)

This means the time spent checking timers grows linearly as you add more timed events.

Common Mistake

[X] Wrong: "Adding more timed events won't affect performance much because each check is simple."

[OK] Correct: Even simple checks add up; many events mean many checks every loop, which slows the program.

Interview Connect

Understanding how multiple timed events affect performance shows you can write efficient Arduino code that scales well as projects grow.

Self-Check

What if we stored timed events in an array and looped through them? How would that change the time complexity?

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