Consider this Arduino sketch that blinks an LED without using delay(). What will be printed to the Serial Monitor?
const int ledPin = 13; unsigned long previousMillis = 0; const long interval = 1000; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; digitalWrite(ledPin, !digitalRead(ledPin)); Serial.println("LED toggled"); } }
Think about how millis() and the time check control when the message prints.
The code uses millis() to check elapsed time and toggles the LED every 1000 milliseconds (1 second). The message prints only when the LED toggles, so it prints once every second.
delay() not recommended in non-blocking code?Which of the following best explains why delay() is avoided in non-blocking Arduino code?
Think about what happens to the program when delay() is called.
delay() pauses the entire program for the specified time, blocking any other code from running. This stops the Arduino from doing multiple things at once, which is why non-blocking code avoids it.
This code is supposed to toggle an LED each time a button is pressed without blocking. What is the bug that prevents it from working correctly?
const int buttonPin = 2; const int ledPin = 13; int ledState = LOW; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { if (digitalRead(buttonPin) == HIGH) { ledState = !ledState; digitalWrite(ledPin, ledState); delay(50); } }
Think about what happens when the button is pressed and the code uses delay().
The delay(50) is used to debounce but it blocks the code, causing the loop to run slowly and the button press to be detected multiple times, toggling the LED rapidly. A non-blocking debounce method is better.
millis()?Choose the code snippet that correctly toggles an LED every 500 milliseconds without blocking.
Consider how to update lastTime to keep consistent timing without drift.
Option B adds 500 to lastTime to keep the timing consistent and avoid drift. Option B resets lastTime to current millis(), which can cause drift. Option B uses wrong comparison and update. Option B resets lastTime to zero, causing repeated immediate toggles.
Given this non-blocking Arduino code, how many times will the LED toggle in 5 seconds?
const int ledPin = 13; unsigned long previousMillis = 0; const long interval = 700; void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis += interval; digitalWrite(ledPin, !digitalRead(ledPin)); } }
Divide the total time by the interval and consider how the code updates previousMillis.
The LED toggles every 700 ms. In 5 seconds (5000 ms), 5000 / 700 ≈ 7.14 intervals. But since previousMillis is incremented by 700 each time, the toggles happen at 700, 1400, 2100, 2800, 3500, 4200, 4900 ms (7 toggles). However, the first toggle happens after 700 ms, so total toggles in 5 seconds is 7. But since the code starts at 0 and toggles after the first interval, the LED toggles 7 times. The closest correct answer is 7 times (option A).