Skip to content
Module: Code Logic

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)

Serial

Start serial communication so the lamp can talk to the computer for debugging.

Sensor Init

Turn on the color sensor and verify it is connected properly via I2C.

LED Test

Run a quick self-test: flash Red, Green, Blue, and White to ensure all lights work.

Reset

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:

SENSE
PROCESS
ACT
SENSE
1

Check the Clock

Has enough time passed since the last scan? If not, wait. This prevents the sensor from overloading.

2

Adjust the Sunglasses

Auto-range: check if the surface is too bright or too dark, and adjust sensitivity accordingly.

3

Take Multiple Snapshots

Quickly capture five different color readings of the surface for accuracy.

PROCESS
4

Filter the Data

Throw away the highest and lowest readings, average the middle three (trimmed mean).

5

Process the Color

Remove grey noise (Black Level Correction), boost vividness (Saturation Boost), adjust for human eyes (Gamma Correction).

ACT
6

Compare Colors

Measure the "distance" between the new color and the current LED color in 3D color space.

7

Take Action

If colors are similar, do nothing (prevents flickering). If different, smoothly fade using linear interpolation.

8

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

The key difference from the Colour Picker: steps 6 and 7 add change detection and smooth transitions. Without these, the lamp would flicker annoyingly or snap jarringly between colors.

Try It!

Try placing the lamp on different colored surfaces and watch the smooth fade. Move it quickly between surfaces to see the change detection in action!