ABC of Electronics cosycom.com
EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 67

Project: RGB Mood Lamp

Mix colours with three PWM channels.

A red, green and blue LED, each behind its own resistor, connect to three PWM pins. Changing each duty cycle mixes any colour.

Our eyes judge brightness unevenly, so a gamma table improves smooth fades. Step through a list of colours with a timer and a state variable.

Add a potentiometer on an ADC pin to control overall brightness.

EXAMPLE
uint8_t level[3] = {255, 80, 0};   /* red, green, blue duty */
void show(void) {
    pwm_set(0, level[0]);   /* hypothetical calls */
    pwm_set(1, level[1]);
    pwm_set(2, level[2]);
}