Consider the following Arduino code using the Blink without delay pattern. What will be the state of the LED connected to pin 13 after 3500 milliseconds?
const int ledPin = 13; unsigned long previousMillis = 0; const long interval = 1000; int ledState = LOW; void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; ledState = (ledState == LOW) ? HIGH : LOW; digitalWrite(ledPin, ledState); } }
Think about how many intervals of 1000 ms have passed after 3500 ms.
The LED toggles every 1000 ms. After 3500 ms, it toggled 3 times (at 1000, 2000, 3000 ms). Starting from LOW, toggling 3 times results in HIGH (ON).
This Arduino sketch uses Blink without delay to toggle an LED and print a message. What will be printed after 2500 milliseconds?
const int ledPin = 13; unsigned long previousMillis = 0; const long interval = 1000; int ledState = LOW; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; ledState = (ledState == LOW) ? HIGH : LOW; digitalWrite(ledPin, ledState); Serial.println(ledState == HIGH ? "LED ON" : "LED OFF"); } }
Count how many times the interval triggers in 2500 ms and what messages print each time.
The interval is 1000 ms. After 2500 ms, the code toggled twice (at 1000 ms and 2000 ms). It prints "LED ON" at 1000 ms and "LED OFF" at 2000 ms.
Examine this Arduino code. The LED never blinks. What is the cause?
const int ledPin = 13; unsigned long previousMillis = 0; const long interval = 1000; int ledState = LOW; void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; ledState = (ledState == LOW) ? HIGH : LOW; digitalWrite(ledPin, ledState); } }
Check the timing condition carefully and how millis() increments.
The condition uses '>' which can skip the exact moment when the difference equals interval. If loop runs slowly or millis() increments in steps, the condition may never be true, so LED never toggles.
Identify the correct fix for the syntax error in this Arduino code snippet:
if (currentMillis - previousMillis >= interval) previousMillis = currentMillis; ledState = (ledState == LOW) ? HIGH : LOW; digitalWrite(ledPin, ledState);
Remember how if statements control multiple lines in Arduino C++.
Without braces, only the first statement after if is conditional. The other two always run, causing logic errors. Adding braces groups all three statements under the if.
You want to blink an LED twice quickly (200 ms ON, 200 ms OFF, twice) then pause for 2 seconds before repeating. Which approach correctly implements this using the Blink without delay pattern?
Think about how to track multiple timed steps without blocking code.
Using a state machine with a step counter and millis() allows precise control of multiple timed ON/OFF states and a pause without blocking the loop.
