LED control patterns in Embedded C - Time & Space Complexity
When controlling LEDs in patterns, it is important to know how the time taken grows as the number of LEDs or steps increases.
We want to find out how the program's running time changes when we change the pattern size.
Analyze the time complexity of the following code snippet.
void led_pattern(int n) {
for (int i = 0; i < n; i++) {
turn_on_led(i);
delay(100);
turn_off_led(i);
}
}
This code turns on each LED one by one, waits a bit, then turns it off, repeating for n LEDs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that turns on and off each LED.
- How many times: It runs exactly n times, once for each LED.
As the number of LEDs (n) increases, the total time grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 on-off cycles |
| 100 | About 100 on-off cycles |
| 1000 | About 1000 on-off cycles |
Pattern observation: Doubling the number of LEDs doubles the work done.
Time Complexity: O(n)
This means the time grows in a straight line with the number of LEDs controlled.
[X] Wrong: "The delay inside the loop makes the time complexity exponential."
[OK] Correct: The delay adds a fixed wait each loop, so total time still grows linearly with n, not exponentially.
Understanding how loops affect time helps you explain how embedded systems handle tasks efficiently.
"What if we nested another loop inside to blink each LED multiple times? How would the time complexity change?"
Practice
Solution
Step 1: Understand LED control patterns
LED control patterns are used to create visual effects by controlling LEDs.Step 2: Identify the purpose in embedded systems
They help show device status or create decorative light sequences by turning LEDs on and off.Final Answer:
To create visual effects by turning LEDs on and off in specific sequences -> Option AQuick Check:
LED patterns = visual effects [OK]
- Confusing LED patterns with data storage
- Thinking LED patterns improve processing speed
- Assuming LED patterns connect to the internet
Solution
Step 1: Recall digitalWrite syntax
The function digitalWrite(pin, value) sets the pin to HIGH or LOW.Step 2: Match correct parameters
Pin number comes first, then the value HIGH to turn LED on.Final Answer:
digitalWrite(5, HIGH); -> Option CQuick Check:
digitalWrite(pin, HIGH) = turn LED on [OK]
- Swapping pin and value order
- Using ON instead of HIGH
- Using lowercase for HIGH
for(int i = 0; i < 3; i++) {
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
delay(500);
}Solution
Step 1: Analyze the loop iterations
The loop runs 3 times, each time turning LED ON then OFF with delays.Step 2: Understand delay timing
Each ON and OFF lasts 500ms, so LED blinks 3 times with equal ON/OFF intervals.Final Answer:
LED blinks 3 times with 0.5 second ON and 0.5 second OFF -> Option DQuick Check:
Loop 3 times with delay = 3 blinks [OK]
- Ignoring the loop count
- Confusing delay units
- Assuming LED stays ON
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, HIGH);
delay(1000);
}Solution
Step 1: Check LED ON/OFF commands
Both digitalWrite calls set pin 2 to HIGH, so LED stays ON.Step 2: Identify missing OFF command
LED never turns OFF because digitalWrite(2, LOW) is missing.Final Answer:
LED never turns OFF because digitalWrite(2, HIGH) is used twice -> Option BQuick Check:
Same HIGH twice = no OFF [OK]
- Assuming delay is wrong
- Forgetting to set pin mode
- Thinking syntax is incorrect
Solution
Step 1: Understand running LED pattern
Running LED means turning ON one LED at a time in sequence, then OFF before next.Step 2: Analyze each option
for(int i = 2; i <= 5; i++) { digitalWrite(i, HIGH); delay(200); digitalWrite(i, LOW); } loops from pin 2 to 5, turns each LED ON, delays, then OFF, creating running effect.Final Answer:
for(int i = 2; i <= 5; i++) { digitalWrite(i, HIGH); delay(200); digitalWrite(i, LOW); } -> Option AQuick Check:
Loop ON then OFF per LED = running pattern [OK]
- Turning all LEDs ON at once
- Not turning off the LED after delay
- Turning LEDs OFF before ON
Ready to prove it?
Pass the test and this pattern moves unknown
