0
0
Arduinoprogramming~20 mins

Non-blocking code architecture in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Non-blocking Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this non-blocking LED blink code?

Consider this Arduino sketch that blinks an LED without using delay(). What will be printed to the Serial Monitor?

Arduino
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");
  }
}
ALED toggled printed every second
BLED toggled printed continuously without delay
CNo output printed because Serial.begin is missing
DLED toggled printed once and then stops
Attempts:
2 left
💡 Hint

Think about how millis() and the time check control when the message prints.

🧠 Conceptual
intermediate
1:30remaining
Why is using delay() not recommended in non-blocking code?

Which of the following best explains why delay() is avoided in non-blocking Arduino code?

A<code>delay()</code> uses too much memory
B<code>delay()</code> only works with LEDs
C<code>delay()</code> causes the Arduino to reset
DBecause <code>delay()</code> stops all code execution, preventing other tasks from running
Attempts:
2 left
💡 Hint

Think about what happens to the program when delay() is called.

🔧 Debug
advanced
2:30remaining
Identify the bug in this non-blocking button press code

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?

Arduino
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);
  }
}
ALED pin is not set as OUTPUT
BButton pin is not set as INPUT_PULLUP
CUsing <code>delay(50)</code> blocks code and causes multiple toggles on a single press
DMissing <code>Serial.begin()</code> in setup
Attempts:
2 left
💡 Hint

Think about what happens when the button is pressed and the code uses delay().

📝 Syntax
advanced
2:30remaining
Which option correctly implements a non-blocking timer using millis()?

Choose the code snippet that correctly toggles an LED every 500 milliseconds without blocking.

A
unsigned long lastTime = 0;
void loop() {
  if (millis() - lastTime &gt; 500) {
    lastTime = lastTime + 500;
    digitalWrite(13, !digitalRead(13));
  }
}
B
unsigned long lastTime = 0;
void loop() {
  if (millis() - lastTime &gt;= 500) {
    lastTime += 500;
    digitalWrite(13, !digitalRead(13));
  }
}
C
unsigned long lastTime = 0;
void loop() {
  if (millis() - lastTime &gt;= 500) {
    lastTime = 0;
    digitalWrite(13, !digitalRead(13));
  }
}
D
unsigned long lastTime = 0;
void loop() {
  if (millis() - lastTime &gt;= 500) {
    lastTime = millis();
    digitalWrite(13, !digitalRead(13));
  }
}
Attempts:
2 left
💡 Hint

Consider how to update lastTime to keep consistent timing without drift.

🚀 Application
expert
3:00remaining
How many times will the LED toggle in 5 seconds with this code?

Given this non-blocking Arduino code, how many times will the LED toggle in 5 seconds?

Arduino
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));
  }
}
A7 times
B5 times
C6 times
D8 times
Attempts:
2 left
💡 Hint

Divide the total time by the interval and consider how the code updates previousMillis.