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
Blink Without Delay Pattern
📖 Scenario: You want to make an LED blink on and off without stopping the rest of your Arduino program. Using the blink without delay pattern helps your program keep running smoothly while blinking the LED.
🎯 Goal: Build a program that blinks an LED connected to pin 13 on and off every 1000 milliseconds (1 second) without using the delay() function.
📋 What You'll Learn
Create a variable to store the LED pin number
Create a variable to store the interval time (1000 milliseconds)
Use millis() to check the time passed
Toggle the LED state without stopping the program
Print the LED state to the Serial Monitor
💡 Why This Matters
🌍 Real World
Blinking LEDs without delay is useful in projects where you want to do multiple things at once, like reading sensors or controlling motors while blinking lights.
💼 Career
Understanding non-blocking code is important for embedded systems programming and real-time applications where delays can cause problems.
Progress0 / 4 steps
1
Set up the LED pin and initial variables
Create an int variable called ledPin and set it to 13. Create an unsigned long variable called previousMillis and set it to 0. Create an int variable called ledState and set it to LOW.
Arduino
Hint
Think about the variables you need to control the LED and track time.
2
Set the blink interval
Create an unsigned long variable called interval and set it to 1000 to represent 1000 milliseconds.
Arduino
Hint
This variable controls how often the LED changes state.
3
Write the blink without delay logic inside loop()
Write a loop() function. Inside it, create an unsigned long variable called currentMillis and set it to millis(). Use an if statement to check if currentMillis - previousMillis is greater than or equal to interval. Inside the if, update previousMillis to currentMillis. Then toggle ledState between LOW and HIGH. Finally, use digitalWrite(ledPin, ledState) to set the LED.
Arduino
Hint
Use millis() to check time and toggle the LED state without delay.
4
Print the LED state to the Serial Monitor
In the setup() function, initialize the serial communication with Serial.begin(9600) and set the ledPin as an output with pinMode(ledPin, OUTPUT). Inside the if statement in loop(), add a Serial.println() statement that prints "LED is ON" when ledState is HIGH and "LED is OFF" when ledState is LOW. This will show the LED status in the Serial Monitor.
Arduino
Hint
Use Serial.begin(9600) in setup() and Serial.println() inside the if to print the LED state.
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
Step 1: Understand delay() limitation
The delay() function pauses the whole program, stopping other tasks.
Step 2: millis() allows multitasking
Using millis() tracks time without stopping the program, so other code runs simultaneously.
Final Answer:
It allows the Arduino to perform other tasks while blinking an LED. -> Option A
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
Step 1: Identify correct data type for millis()
millis() returns an unsigned long value representing milliseconds.
Step 2: Match variable type to millis()
To store millis() values, use unsigned long to avoid overflow and negative values.
Final Answer:
unsigned long previousMillis = 0; -> Option A
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
Step 1: Analyze timing logic
The code checks if 1000 ms passed since last toggle, then flips ledState.
Step 2: Understand LED toggle and output
ledState toggles true/false every second, controlling LED on/off without delay blocking.
Final Answer:
The LED on pin 13 will blink on and off every 1 second without stopping other code. -> Option B
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
Step 1: Check use of digitalRead on output pin
digitalRead() on an output pin is unreliable and not recommended for toggling.
Step 2: Understand proper toggle method
Better to track LED state in a variable rather than reading output pin state.
Final Answer:
digitalRead() cannot be used on output pins. -> Option D
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
Step 1: Understand independent timing needs
Each LED needs its own timer variable to track its blinking interval separately.
Step 2: Avoid blocking delays and incorrect loops
Using delay() or nested loops blocks code and prevents independent blinking.
Final Answer:
Use two separate previousMillis variables and check each interval independently in loop(). -> Option C
Quick Check:
Separate timers for independent blinking [OK]
Hint: Track each LED's time separately with its own variable [OK]