RGB LED color mixing in Raspberry Pi - Time & Space Complexity
When mixing colors on an RGB LED, the program changes red, green, and blue values to create new colors.
We want to know how the time to mix colors grows as we try more color combinations.
Analyze the time complexity of the following code snippet.
for red in range(0, 256, 51):
for green in range(0, 256, 51):
for blue in range(0, 256, 51):
set_rgb_led(red, green, blue)
delay(100)
This code cycles through red, green, and blue values in steps of 51 to mix colors on an RGB LED.
- Primary operation: The three nested loops each run through color values.
- How many times: Each loop runs 6 times (0, 51, 102, 153, 204, 255), so total calls are 6 x 6 x 6 = 216.
As the number of color steps increases, the total operations grow quickly because of the three loops.
| Input Size (steps per color) | Approx. Operations |
|---|---|
| 6 | 216 |
| 10 | 1000 |
| 20 | 8000 |
Pattern observation: The total operations grow by the cube of the number of steps because of three nested loops.
Time Complexity: O(n³)
This means if you double the number of color steps, the time to mix colors grows eight times.
[X] Wrong: "The time grows linearly because we just add red, green, and blue loops."
[OK] Correct: The loops are nested, so the total time multiplies, not adds, making the growth much faster.
Understanding how nested loops affect time helps you explain performance in real projects, like controlling hardware or processing images.
"What if we combined the three loops into one loop that sets all colors at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand PWM role in LEDs
PWM (Pulse Width Modulation) changes how long the LED is on versus off, controlling brightness.Step 2: Apply PWM to RGB LED components
Each color (red, green, blue) brightness is controlled separately by PWM signals.Final Answer:
The brightness of each red, green, and blue LED component -> Option AQuick Check:
PWM controls brightness = D [OK]
- Confusing PWM with voltage control
- Thinking PWM controls processor speed
- Assuming PWM changes LED temperature
Solution
Step 1: Recall RPi.GPIO PWM syntax
The correct syntax is to create a PWM object with GPIO.PWM(pin, frequency).Step 2: Start PWM with duty cycle
Then call pwm.start(duty_cycle) to begin PWM signal.Final Answer:
pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle) -> Option BQuick Check:
Correct PWM start syntax = C [OK]
- Using non-existent methods like PWMStart or startPWM
- Calling start before creating PWM object
- Mixing method names or parameters
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) red = 17 green = 27 blue = 22 GPIO.setup(red, GPIO.OUT) GPIO.setup(green, GPIO.OUT) GPIO.setup(blue, GPIO.OUT) pwm_red = GPIO.PWM(red, 1000) pwm_green = GPIO.PWM(green, 1000) pwm_blue = GPIO.PWM(blue, 1000) pwm_red.start(100) pwm_green.start(0) pwm_blue.start(0)What color will the RGB LED show?
Solution
Step 1: Analyze PWM duty cycles for each color
Red PWM is started at 100% duty cycle (full brightness), green and blue at 0% (off).Step 2: Determine resulting LED color
Only red LED is on fully, so the LED shows bright red color.Final Answer:
Bright red -> Option DQuick Check:
Red=100%, Green=0%, Blue=0% = Bright red [OK]
- Assuming 0 means half brightness
- Mixing pin numbers with colors
- Thinking all LEDs light up by default
pwm_red.start(50) pwm_green.start(50) pwm_blue.start(0) GPIO.cleanup()What is the main issue causing the LED to not show the mixed color?
Solution
Step 1: Understand GPIO.cleanup() effect
GPIO.cleanup() resets all GPIO pins, stopping PWM signals immediately.Step 2: Identify timing issue in code
Calling cleanup right after starting PWM stops the LEDs before visible mixing.Final Answer:
Calling GPIO.cleanup() immediately stops PWM signals -> Option AQuick Check:
cleanup() stops PWM = A [OK]
- Thinking duty cycle must be 0 or 100 only
- Ignoring cleanup() effect on GPIO pins
- Assuming frequency defaults to zero
Solution
Step 1: Understand color fading by PWM mixing
Fading from red to blue means red brightness decreases while blue brightness increases smoothly.Step 2: Apply PWM duty cycle changes
Decrease red duty cycle from 100% to 0%, increase blue from 0% to 100%, green stays off.Final Answer:
Gradually decrease red duty cycle from 100 to 0 while increasing blue from 0 to 100 -> Option CQuick Check:
Fade red down, blue up = A [OK]
- Turning both colors fully on causes white, not fade
- Blinking one color doesn't create smooth fade
- Keeping all colors constant prevents fading
Ready to prove it?
Pass the test and this pattern moves unknown
