How It Works
The Chameleon Lamp has a two-phase architecture: a one-time setup when it powers on, followed by a continuous loop that never stops running.
The DJ Analogy
"Think of the lamp like a DJ at a party. First, the DJ sets up their equipment and does a sound check (Phase 1). Then they continuously read the crowd, sense the vibe, and smoothly transition between songs to keep the energy right (Phase 2). The lamp does the same — but with colors instead of music!"
Phase 1: Waking Up (Setup)
Start serial communication so the lamp can talk to the computer for debugging.
Turn on the color sensor and verify it is connected properly via I2C.
Run a quick self-test: flash Red, Green, Blue, and White to ensure all lights work.
Set the starting color to pitch black. The lamp is now ready to begin sensing.
Phase 2: The Continuous Loop
Once setup is complete, the lamp enters an infinite loop. Every cycle runs through these 8 steps:
Check the Clock
Has enough time passed since the last scan? If not, wait. This prevents the sensor from overloading.
Adjust the Sunglasses
Auto-range: check if the surface is too bright or too dark, and adjust sensitivity accordingly.
Take Multiple Snapshots
Quickly capture five different color readings of the surface for accuracy.
Filter the Data
Throw away the highest and lowest readings, average the middle three (trimmed mean).
Process the Color
Remove grey noise (Black Level Correction), boost vividness (Saturation Boost), adjust for human eyes (Gamma Correction).
Compare Colors
Measure the "distance" between the new color and the current LED color in 3D color space.
Take Action
If colors are similar, do nothing (prevents flickering). If different, smoothly fade using linear interpolation.
Repeat
Go back to step 1. This loop runs forever!
Loop back to Step 1 — this cycle runs forever, always watching and adapting.
Key Takeaway
Try It!