Challenge - 5 Problems
Millis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of multiple timed events with millis()
What will be the output on the serial monitor after running this Arduino code for about 3500 milliseconds?
Arduino
unsigned long previousMillis1 = 0; unsigned long previousMillis2 = 0; const long interval1 = 1000; const long interval2 = 2000; void setup() { Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis1 >= interval1) { previousMillis1 = currentMillis; Serial.println("Event 1"); } if (currentMillis - previousMillis2 >= interval2) { previousMillis2 = currentMillis; Serial.println("Event 2"); } }
Attempts:
2 left
💡 Hint
Think about how often each event triggers based on its interval and the elapsed time.
✗ Incorrect
Event 1 triggers every 1000 ms, Event 2 triggers every 2000 ms. So at 1000 ms: Event 1, at 2000 ms: Event 1 and Event 2, at 3000 ms: Event 1 again. This matches option A.
🧠 Conceptual
intermediateUnderstanding millis() rollover
What happens when the Arduino millis() function reaches its maximum value and rolls over to zero? How should you write your timing code to handle this correctly?
Attempts:
2 left
💡 Hint
Think about how unsigned arithmetic works in C++.
✗ Incorrect
Because millis() returns an unsigned long, when it rolls over from its max value to zero, subtraction still works correctly due to unsigned integer wraparound. This allows timing code using (currentMillis - previousMillis) to work seamlessly.
🔧 Debug
advancedWhy does this multiple event timing code fail?
This Arduino code is supposed to blink two LEDs at different intervals using millis(), but only one LED blinks. What is the problem?
Arduino
unsigned long previousMillis1 = 0; unsigned long previousMillis2 = 0; const long interval1 = 500; const long interval2 = 1000; void setup() { pinMode(9, OUTPUT); pinMode(10, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis1 >= interval1) { previousMillis1 = currentMillis; digitalWrite(9, !digitalRead(9)); } if (currentMillis - previousMillis2 >= interval2) { previousMillis2 = currentMillis; digitalWrite(10, !digitalRead(10)); } }
Attempts:
2 left
💡 Hint
Check how previousMillis is updated in both conditions.
✗ Incorrect
Using the same previousMillis variable for both intervals causes the second if condition to fail because previousMillis is updated in the first if, resetting the timer for the second. Separate variables are needed.
📝 Syntax
advancedIdentify the syntax error in this millis() timing code
Which option contains the correct syntax to check if 1000 milliseconds have passed using millis()?
Attempts:
2 left
💡 Hint
Check the comparison operators and semicolon placement.
✗ Incorrect
Option A uses the correct '>=' operator and no extra semicolon after the if condition. Option A uses invalid operator '=>', C uses assignment '=', and D has a semicolon after if condition causing block to always run.
🚀 Application
expertImplement three independent timed events with millis()
You want to toggle three LEDs connected to pins 3, 5, and 6 at intervals of 300 ms, 700 ms, and 1500 ms respectively using millis(). Which code snippet correctly implements this without blocking?
Attempts:
2 left
💡 Hint
Each LED needs its own timer variable and toggling the state.
✗ Incorrect
Option D correctly uses separate previousMillis variables and toggles each LED state independently. Option D uses one variable for all, causing timing conflicts. Options C and D only set HIGH or LOW without toggling, so LEDs won't blink.
