This pattern helps you blink an LED without stopping the rest of your program. It avoids using delay, so your program can do many things at once.
Blink without delay pattern in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
unsigned long previousMillis = 0; const long interval = 1000; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // toggle LED here } }
millis() returns the number of milliseconds since the Arduino started.
We check if enough time has passed by subtracting previous time from current time.
unsigned long previousMillis = 0; const long interval = 500; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } }
unsigned long previousMillis = 0; const long interval = 2000; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }
This program blinks the built-in LED on and off every 1 second without using delay. The rest of the program can run without pause.
const int ledPin = LED_BUILTIN; 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; if (digitalRead(ledPin) == LOW) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } } }
Using delay() stops your program and can make it unresponsive.
millis() keeps counting even if you do other things, so it helps track time without stopping.
Make sure to use unsigned long for time variables to avoid errors when millis() rolls over.
Blink without delay lets your Arduino do many things at once.
Use millis() to check time passed instead of delay().
This pattern makes your programs more responsive and flexible.
Practice
Blink without delay pattern in Arduino programming?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 AQuick Check:
Blink without delay = multitasking [OK]
- Thinking delay() lets other code run
- Believing blink speed is faster with millis()
- Assuming millis() uses less power
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, useunsigned longto avoid overflow and negative values.Final Answer:
unsigned long previousMillis = 0; -> Option AQuick Check:
Use unsigned long for time tracking [OK]
- Using int which can overflow quickly
- Using float which is unnecessary and imprecise
- Using boolean which can't store time
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);
}
}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 BQuick Check:
Millis timing toggles LED every 1 second [OK]
- Thinking bool causes compile error
- Assuming LED stays on permanently
- Confusing rapid blinking with 1-second interval
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));
}
}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 DQuick Check:
Don't use digitalRead on output pins [OK]
- Thinking > vs >= causes major error
- Believing previousMillis type is wrong
- Assuming digitalWrite logic causes runtime error
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 CQuick Check:
Separate timers for independent blinking [OK]
- Using one timer for both LEDs
- Using delay() which blocks code
- Trying nested loops for timing
