Bird
Raised Fist0
Arduinoprogramming~5 mins

Multiple timed events with millis() in Arduino - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What does the millis() function return in Arduino?

millis() returns the number of milliseconds since the Arduino board began running the current program.

Click to reveal answer
beginner
Why is using delay() not ideal for multiple timed events?

delay() pauses the entire program, stopping all actions. This makes it hard to run multiple events at once.

Click to reveal answer
intermediate
How can you track multiple timed events using millis()?

By saving the last time each event happened and checking if enough time has passed using millis(), you can run each event independently without stopping the program.

Click to reveal answer
beginner
What is the purpose of storing a variable like previousMillis in timing code?

previousMillis stores the last time an event occurred, so you can compare it with the current millis() to decide if the event should run again.

Click to reveal answer
advanced
What happens when millis() overflows after about 50 days?

millis() resets to zero after about 50 days, but timing code using subtraction still works correctly because of how unsigned numbers behave.

Click to reveal answer
What does millis() return in Arduino?
ACurrent time of day
BSeconds since the program started
CMilliseconds since the program started
DMicroseconds since the program started
Why is delay() not good for multiple timed events?
AIt speeds up the program
BIt pauses the whole program
CIt only works once
DIt uses too much memory
How do you check if enough time has passed for an event using millis()?
ACompare current <code>millis()</code> with stored previous time
BUse <code>delay()</code> for the event duration
CReset <code>millis()</code> manually
DUse <code>micros()</code> instead
What variable name is commonly used to store the last event time?
AtimerCount
BlastDelay
CeventTime
DpreviousMillis
What happens when millis() overflows after ~50 days?
ATiming still works correctly due to unsigned math
BProgram crashes
CTiming stops working
DArduino resets automatically
Explain how to use millis() to run two different timed events without stopping the program.
Think about saving times and checking intervals separately for each event.
You got /4 concepts.
    Describe why using delay() is not suitable for multiple timed events and how millis() solves this problem.
    Consider what happens when the program is paused.
    You got /4 concepts.

      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