Bird
Raised Fist0
Arduinoprogramming~20 mins

Blink without delay pattern in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Blink without delay 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 Blink without delay code?

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?

Arduino
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);
  }
}
AThe LED will blink rapidly
BThe LED will be OFF
CThe LED will be ON
DThe LED state is undefined
Attempts:
2 left
💡 Hint

Think about how many intervals of 1000 ms have passed after 3500 ms.

Predict Output
intermediate
2:00remaining
What will this code print to Serial Monitor?

This Arduino sketch uses Blink without delay to toggle an LED and print a message. What will be printed after 2500 milliseconds?

Arduino
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");
  }
}
A"LED ON" printed 3 times
B"LED ON" and "LED OFF" printed alternating twice
C"LED ON" and "LED OFF" printed alternating three times
DNo output printed
Attempts:
2 left
💡 Hint

Count how many times the interval triggers in 2500 ms and what messages print each time.

🔧 Debug
advanced
2:00remaining
Why does this Blink without delay code fail to blink the LED?

Examine this Arduino code. The LED never blinks. What is the cause?

Arduino
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);
  }
}
AThe condition uses '>' instead of '>=' causing missed toggles
BThe variable ledState is not initialized properly
CThe pinMode is set incorrectly
DThe interval variable is too short
Attempts:
2 left
💡 Hint

Check the timing condition carefully and how millis() increments.

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this Blink without delay snippet?

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);
AAdd curly braces around the three statements after if
BAdd a semicolon after the if condition
CIndent the last two statements to align with if
DReplace the if with a while loop
Attempts:
2 left
💡 Hint

Remember how if statements control multiple lines in Arduino C++.

🚀 Application
expert
3:00remaining
How to modify Blink without delay to blink LED twice quickly then pause?

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?

AUse delay(200) twice then delay(2000) inside loop()
BUse millis() to toggle LED every 1000 ms and ignore the quick double blink
CToggle LED every 200 ms and count toggles, then delay(2000) after 4 toggles
DUse a state machine with a step counter and millis() checks for each ON/OFF and pause interval
Attempts:
2 left
💡 Hint

Think about how to track multiple timed steps without blocking code.

Practice

(1/5)
1. What is the main advantage of using the Blink without delay pattern in Arduino programming?
easy
A. It allows the Arduino to perform other tasks while blinking an LED.
B. It makes the LED blink faster than usual.
C. It uses less power than the delay() function.
D. It requires fewer lines of code than delay().

Solution

  1. Step 1: Understand delay() limitation

    The delay() function pauses the whole program, stopping other tasks.
  2. Step 2: millis() allows multitasking

    Using millis() tracks time without stopping the program, so other code runs simultaneously.
  3. Final Answer:

    It allows the Arduino to perform other tasks while blinking an LED. -> Option A
  4. Quick Check:

    Blink without delay = multitasking [OK]
Hint: Remember millis() tracks time without stopping code [OK]
Common Mistakes:
  • Thinking delay() lets other code run
  • Believing blink speed is faster with millis()
  • Assuming millis() uses less power
2. Which of the following code snippets correctly initializes the variable to store the last time the LED was toggled in the Blink without delay pattern?
easy
A. unsigned long previousMillis = 0;
B. int previousMillis = 0;
C. float previousMillis = 0.0;
D. boolean previousMillis = false;

Solution

  1. Step 1: Identify correct data type for millis()

    millis() returns an unsigned long value representing milliseconds.
  2. Step 2: Match variable type to millis()

    To store millis() values, use unsigned long to avoid overflow and negative values.
  3. Final Answer:

    unsigned long previousMillis = 0; -> Option A
  4. Quick Check:

    Use unsigned long for time tracking [OK]
Hint: Use unsigned long for millis() time variables [OK]
Common Mistakes:
  • Using int which can overflow quickly
  • Using float which is unnecessary and imprecise
  • Using boolean which can't store time
3. What will be the output behavior of the following Arduino code snippet using Blink without delay pattern?
const int ledPin = 13;
unsigned long previousMillis = 0;
const long interval = 1000;
bool ledState = false;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(ledPin, ledState ? HIGH : LOW);
  }
}
medium
A. The LED will blink rapidly without delay.
B. The LED on pin 13 will blink on and off every 1 second without stopping other code.
C. The LED will stay on permanently after 1 second.
D. The program will cause a compile error due to bool type.

Solution

  1. Step 1: Analyze timing logic

    The code checks if 1000 ms passed since last toggle, then flips ledState.
  2. Step 2: Understand LED toggle and output

    ledState toggles true/false every second, controlling LED on/off without delay blocking.
  3. Final Answer:

    The LED on pin 13 will blink on and off every 1 second without stopping other code. -> Option B
  4. Quick Check:

    Millis timing toggles LED every 1 second [OK]
Hint: Look for millis() interval check to predict blink timing [OK]
Common Mistakes:
  • Thinking bool causes compile error
  • Assuming LED stays on permanently
  • Confusing rapid blinking with 1-second interval
4. Identify the error in this Blink without delay code snippet:
const int ledPin = 13;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}
medium
A. The code will cause a runtime error due to digitalWrite logic.
B. Using > instead of >= in the if condition causes timing issues.
C. The variable previousMillis should be int, not unsigned long.
D. digitalRead() cannot be used on output pins.

Solution

  1. Step 1: Check use of digitalRead on output pin

    digitalRead() on an output pin is unreliable and not recommended for toggling.
  2. Step 2: Understand proper toggle method

    Better to track LED state in a variable rather than reading output pin state.
  3. Final Answer:

    digitalRead() cannot be used on output pins. -> Option D
  4. Quick Check:

    Don't use digitalRead on output pins [OK]
Hint: Avoid digitalRead on pins set as OUTPUT [OK]
Common Mistakes:
  • Thinking > vs >= causes major error
  • Believing previousMillis type is wrong
  • Assuming digitalWrite logic causes runtime error
5. You want to blink two LEDs independently using the Blink without delay pattern: LED1 every 500 ms and LED2 every 1000 ms. Which approach correctly manages both LEDs without blocking the program?
hard
A. Toggle LED1 every 500 ms and LED2 every 1000 ms using nested for loops.
B. Use one previousMillis variable and toggle both LEDs at the same time every 500 ms.
C. Use two separate previousMillis variables and check each interval independently in loop().
D. Use delay(500) for LED1 and delay(1000) for LED2 in sequence inside loop().

Solution

  1. Step 1: Understand independent timing needs

    Each LED needs its own timer variable to track its blinking interval separately.
  2. Step 2: Avoid blocking delays and incorrect loops

    Using delay() or nested loops blocks code and prevents independent blinking.
  3. Final Answer:

    Use two separate previousMillis variables and check each interval independently in loop(). -> Option C
  4. Quick Check:

    Separate timers for independent blinking [OK]
Hint: Track each LED's time separately with its own variable [OK]
Common Mistakes:
  • Using one timer for both LEDs
  • Using delay() which blocks code
  • Trying nested loops for timing