Complete the code to make the LED blink every second.
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay([1]);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The delay(1000) pauses the program for 1000 milliseconds, or 1 second, making the LED stay on for that time.
Complete the code to measure elapsed time without stopping the program.
unsigned long previousMillis = 0; unsigned long interval = 1000; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= [1]) { previousMillis = currentMillis; // do something } }
The code checks if 1000 milliseconds (1 second) have passed to perform an action without stopping the program.
Fix the error in the timing control code to avoid blocking the loop.
void loop() {
delay([1]);
// other code that needs to run frequently
}Using delay(0) avoids blocking the loop, allowing other code to run frequently.
Fill both blanks to create a non-blocking LED blink using millis().
unsigned long previousMillis = 0; const long interval = 1000; void loop() { unsigned long currentMillis = [1](); if (currentMillis - previousMillis >= [2]) { previousMillis = currentMillis; // toggle LED } }
millis() returns the current time in milliseconds, and interval is the time to wait before toggling the LED.
Fill all three blanks to create a dictionary-like structure that stores sensor values with timing condition.
struct SensorData {
String name;
int value;
};
SensorData data[] = {
{"temp", [1],
{"light", [2]
};
void loop() {
if (millis() [3] 1000) {
// update sensor values
}
}Initialize temp sensor value to 0, light sensor value with analogRead(A0), and check if millis() is greater than 1000 to update values.
