0
0
Arduinoprogramming~10 mins

Non-blocking code architecture in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if 1 second has passed without blocking.

Arduino
unsigned long currentMillis = millis();
if (currentMillis - previousMillis [1] interval) {
  previousMillis = currentMillis;
  // action to perform
}
Drag options to blanks, or click blank then click option'
A>=
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which may miss the exact millisecond.
Using '<' which checks the wrong condition.
2fill in blank
medium

Complete the code to update the LED state without delay.

Arduino
if (millis() - previousMillis >= [1]) {
  previousMillis = millis();
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
}
Drag options to blanks, or click blank then click option'
A500
Bdelay
C1000
Dmillis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delay' which blocks the program.
Using 'millis' which is a function, not a number.
3fill in blank
hard

Fix the error in the non-blocking blink code by completing the blank.

Arduino
unsigned long previousMillis = 0;
const long interval = 1000;
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis [1] interval) {
    previousMillis = currentMillis;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
}
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which causes the condition to never be true.
Using '==' which may miss the exact timing.
4fill in blank
hard

Fill both blanks to create a non-blocking timer that toggles an LED every 2 seconds.

Arduino
unsigned long previousMillis = 0;
const long [1] = 2000;
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis [2] interval) {
    previousMillis = currentMillis;
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}
Drag options to blanks, or click blank then click option'
Ainterval
Bdelay
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delay' instead of a variable name.
Using '<' which causes wrong timing.
5fill in blank
hard

Fill all three blanks to create a non-blocking code that toggles an LED every 500ms and reads a button state.

Arduino
unsigned long previousMillis = 0;
const long [1] = 500;
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis [2] interval) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }
  buttonState = digitalRead([3]);
}
Drag options to blanks, or click blank then click option'
Ainterval
B>=
CbuttonPin
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delay' which blocks the code.
Using '<' which causes wrong timing.
Using wrong pin name for button.