Multiple LED patterns in Raspberry Pi - Time & Space Complexity
When controlling multiple LED patterns on a Raspberry Pi, it is important to understand how the time to run the program changes as we add more LEDs or patterns.
We want to know how the program's running time grows when we increase the number of LEDs or patterns.
Analyze the time complexity of the following code snippet.
for pattern in patterns:
for led in leds:
turn_on(led)
wait(delay)
turn_off(led)
This code runs multiple LED patterns by turning each LED on and off in sequence for each pattern.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over patterns and LEDs.
- How many times: The inner loop runs once for each LED, repeated for each pattern.
As the number of patterns or LEDs increases, the total operations grow by multiplying these counts.
| Input Size (patterns × LEDs) | Approx. Operations |
|---|---|
| 10 × 5 | 50 |
| 100 × 5 | 500 |
| 100 × 100 | 10,000 |
Pattern observation: Doubling the number of patterns or LEDs roughly doubles the total operations.
Time Complexity: O(p × l)
This means the running time grows proportionally to the number of patterns times the number of LEDs.
[X] Wrong: "The time grows only with the number of LEDs, not patterns."
[OK] Correct: Because the code runs through all LEDs for each pattern, both counts multiply to affect total time.
Understanding how nested loops affect running time helps you explain how your code scales, a key skill in programming and problem solving.
"What if we combined all patterns into one loop instead of nesting? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of LED patterns
LED patterns are used to create visual effects that make projects engaging.Step 2: Identify the correct purpose
The main goal is to make the project interactive and fun, not to waste power or harm the device.Final Answer:
To make the project more interactive and fun -> Option CQuick Check:
LED patterns = fun and interactive [OK]
- Confusing LED patterns with hardware issues
- Assuming LEDs always consume too much power
- Thinking LEDs slow down the processor
Solution
Step 1: Review GPIO setup syntax
GPIO.setup can accept a list of pins and a mode like GPIO.OUT to set multiple pins at once.Step 2: Compare options
GPIO.setup([17, 18, 27], GPIO.OUT) correctly uses a list and GPIO.OUT. GPIO.setup(17, 18, 27, GPIO.OUT) has wrong argument format. GPIO.setup(17, GPIO.OUT); GPIO.setup(18, GPIO.OUT); GPIO.setup(27, GPIO.IN) incorrectly sets pin 27 as input. GPIO.setup([17, 18, 27], GPIO.IN) sets pins as input, not output.Final Answer:
GPIO.setup([17, 18, 27], GPIO.OUT) -> Option BQuick Check:
List of pins + GPIO.OUT = correct setup [OK]
- Passing multiple pins as separate arguments
- Setting pins as input instead of output
- Not using a list for multiple pins
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup([17, 18], GPIO.OUT)
for pin in [17, 18]:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin, GPIO.LOW)
GPIO.cleanup()Solution
Step 1: Analyze the for loop behavior
The loop turns on pin 17 LED, waits 0.5 seconds, then turns it off, then repeats for pin 18.Step 2: Understand timing and output
LEDs blink one after another, not together, each for 0.5 seconds.Final Answer:
LED on pin 17 turns on for 0.5s, then off; then LED on pin 18 turns on for 0.5s, then off -> Option AQuick Check:
Sequential LED blinking = LED on pin 17 turns on for 0.5s, then off; then LED on pin 18 turns on for 0.5s, then off [OK]
- Assuming LEDs blink simultaneously
- Confusing pin numbers
- Thinking code has syntax errors
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup([17, 18], GPIO.OUT)
for pin in [17, 18]:
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
GPIO.cleanup()Solution
Step 1: Check LED on/off control in loop
The code turns LEDs on but never turns them off, so LEDs stay lit.Step 2: Fix by turning LEDs off after delay
Adding GPIO.output(pin, GPIO.LOW) after sleep turns LEDs off properly.Final Answer:
Add GPIO.output(pin, GPIO.LOW) inside the loop after sleep -> Option DQuick Check:
Turn off LEDs after on + delay [OK]
- Forgetting to turn off LEDs
- Setting pins as input instead of output
- Removing cleanup causing resource issues
Solution
Step 1: Understand chase pattern requirements
Each LED lights up one by one with delay, then all turn off together.Step 2: Analyze options for correct sequence
for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.HIGH) time.sleep(0.3) for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.LOW) lights LEDs one by one with delay, then turns all off together. for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.HIGH) time.sleep(0.3) GPIO.output(pin, GPIO.LOW) turns each LED on and off immediately, no chase. GPIO.output([17, 18, 27, 22], GPIO.HIGH) time.sleep(0.3) GPIO.output([17, 18, 27, 22], GPIO.LOW) turns all on at once, no chase. for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.LOW) time.sleep(0.3) GPIO.output([17, 18, 27, 22], GPIO.HIGH) turns LEDs off first, then all on, wrong order.Final Answer:
for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.HIGH) time.sleep(0.3) for pin in [17, 18, 27, 22]: GPIO.output(pin, GPIO.LOW) -> Option AQuick Check:
Sequential on + delay, then all off [OK]
- Turning LEDs off immediately inside loop
- Turning all LEDs on at once instead of sequentially
- Reversing on/off order
Ready to prove it?
Pass the test and this pattern moves unknown
