Software

The code for the RGB LED color mixer is written in C with the WINAVR C-Compiler The hardware PWM timers of the ATMega328 are used to let the LED's light. To use the PWM timers you first need to initialize the timers at the begin of the program code. The value of the potmeters is read by the adc converter and assigned to the corresponding PWM that will light up the LED's. You can copy and paste the textfile into the WINAVR C-compiler. Below you find the C code.

 

// program name: RGB LED Color Mixer
// date: 12-07-2016
// target device: ATMeg328
// purpose: mix RGB LED to 8 colors with 3 potmeters, uses ADC channels input.
// author: www.avrprojects.net
//

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

// this function read the value of the selected ADC channel
unsigned char read_adc(unsigned char channel)
{
ADMUX = 0b00100000; // set ADC0
ADMUX |= channel; // set the adc channel
ADCSRA = 0b10000111; //set ADEN, precale by 128
ADCSRA |= 1<<ADSC; //start conversion;
while (ADCSRA&(1<<ADSC)); //wait conversion end
return ADCH;
}

// main program
int main (void)
{
//PWM1&2 initialisation
TCCR1A = 0b10100001;// set PWM mode for PB1 and PB2
TCCR1B = 0b00000010; //set clock:8 prescaler

//PWM3 initialisation
TCCR2A = 0b10000001; // set PWM mode for PB3
TCCR2B = 0b00000010; //set clock:8 prescaler

DDRB = 0xFF; //set portB as output

while(1) //infinite loop
{
OCR1A = read_adc(0); // reads the adc channel and assigns it to the corresponding PWM
OCR1B = read_adc(1);
OCR2A = read_adc(2);
_delay_ms(50);
}

}