LED blink pattern in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we write code to blink an LED repeatedly, it is important to understand how the time the program takes grows as we increase the number of blinks.
We want to know how the total work changes when we ask the LED to blink more times.
Analyze the time complexity of the following code snippet.
const int ledPin = 13;
const int blinkCount = 100;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < blinkCount; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
while(true) {}
}
This code makes an LED blink on and off 100 times, with a half-second pause each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that repeats the blink steps.
- How many times: It runs exactly
blinkCounttimes, which is 100 in this example.
Each blink takes a fixed amount of time, so if we double the number of blinks, the total time doubles too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 blinks (10 loops) |
| 100 | 100 blinks (100 loops) |
| 1000 | 1000 blinks (1000 loops) |
Pattern observation: The total work grows directly in proportion to the number of blinks.
Time Complexity: O(n)
This means the time to complete the blinking grows linearly with the number of blinks.
[X] Wrong: "The blinking time stays the same no matter how many times the LED blinks."
[OK] Correct: Each blink takes time, so more blinks mean more total time. The work adds up, not stays fixed.
Understanding how loops affect time helps you explain how your code scales. This skill is useful when writing programs that repeat actions many times, like blinking LEDs or reading sensors.
"What if we changed the delay time inside the loop? How would the time complexity change?"
Practice
delay(1000); command do in an Arduino LED blink program?Solution
Step 1: Understand the delay function
Thedelay()function pauses the program for the given time in milliseconds.Step 2: Interpret the argument 1000
1000 milliseconds equals 1 second, so the program waits for 1 second before continuing.Final Answer:
Pauses the program for 1000 milliseconds (1 second) -> Option AQuick Check:
delay(1000) = 1 second pause [OK]
- Thinking delay turns LED on or off
- Confusing delay time with brightness
- Assuming delay is in seconds
Solution
Step 1: Recall pinMode syntax
The correct syntax ispinMode(pin, mode);where pin is the pin number and mode is INPUT or OUTPUT.Step 2: Match the correct order
pinMode(13, OUTPUT); usespinMode(13, OUTPUT);which matches the correct order and parameters.Final Answer:
pinMode(13, OUTPUT); -> Option BQuick Check:
pinMode(pin, OUTPUT) sets pin as output [OK]
- Swapping pin and mode parameters
- Using digitalWrite instead of pinMode to set mode
- Missing semicolon at end
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}Solution
Step 1: Analyze the delay times
The LED is turned on, then the program waits 500 ms, then turned off, then waits 500 ms again.Step 2: Calculate total blink cycle
On time + off time = 500 ms + 500 ms = 1000 ms (1 second) per full blink cycle. Each on or off state lasts 0.5 seconds.Final Answer:
LED on pin 13 blinks on and off every 1 second -> Option DQuick Check:
delay(500) means 0.5 second blink intervals [OK]
- Confusing total blink time with single delay
- Ignoring delay after turning LED off
- Assuming delay is in seconds
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000)
digitalWrite(13, LOW);
delay(1000);
}Solution
Step 1: Check syntax line by line
Look at each statement for missing semicolons or syntax errors.Step 2: Find missing semicolon
The linedelay(1000)is missing a semicolon at the end, causing a syntax error.Final Answer:
Missing semicolon after delay(1000) -> Option AQuick Check:
Every statement must end with ; [OK]
- Putting pinMode inside loop instead of setup
- Thinking delay can't be used in loop
- Ignoring missing semicolon errors
Solution
Step 1: Understand the blink pattern
The LED should blink twice quickly (short on/off), then pause 2 seconds before repeating.Step 2: Analyze each option's timing
for(int i=0; i<2; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); } delay(2000); blinks twice with 200 ms on and off delays, then pauses 2000 ms. This matches the pattern.Step 3: Check other options
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); delay(2000); blinks once only. for(int i=0; i<2; i++) { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); } delay(2000); blinks twice but with 1 second delays (too slow). digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); delay(2000); blinks once with 500 ms delays.Final Answer:
for(int i=0; i<2; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); } delay(2000); -> Option CQuick Check:
Loop twice fast blinks + long pause = for(int i=0; i<2; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); } delay(2000); [OK]
- Using delay too long for quick blinks
- Not looping for multiple blinks
- Pausing before blinking instead of after
