Expand on RGB LED dimmer code by allowing for advanced serial communication, and add a diffuser to blend RGB LED colors. My code allows you to control each LED in 20% brightness increments, with R using "1-6", G using "q-y", and B using "a-h". I did this because as a designer, I'm used to picking colors by hue, saturation, and brightness (HSB), but specify the colors in RGB for a developer to use. This interface allows for a way to understand how RGB color mixing works. The diffuser I used was a plastic bottle stopper. I found that for better color mixing, the important thing is to point all three LEDs so they point to the same spot.
// Ian Leighton // TUI 2011-09-13 // Homework: Serial RGB LED Keyboard Dimmer /* Dims RGB LEDs in 20% increments using 1-6 Red q-y Green a-h Blue */ int colorP; int bright; char in; int brightness; #define redPin11 // Red LED, connected to digital pin 9 #define greenPin 10 // Green LED, connected to digital pin 10 #define bluePin 9 // Blue LED, connected to digital pin 11 void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); analogWrite(redPin, 0); // set them all to mid brightness analogWrite(greenPin, 0); // set them all to mid brightness analogWrite(bluePin, 0); // set them all to mid brightness Serial.println("enter a color value using the first three row of the keyboard (e.g. '3rk') :"); } void loop () { in = Serial.read(); if (in != -1) { // check color if(in == '1' || in=='2' || in=='3' || in=='4' || in=='5' || in=='6') { colorP = redPin; Serial.print("R = "); } if(in=='q' || in=='w' || in=='e' || in=='r' || in=='t' || in=='y') { colorP = greenPin; Serial.print("G = "); } if(in=='a' || in=='s' || in=='d' || in=='f' || in=='g' || in=='h') { colorP = bluePin; Serial.print("B = "); } // check brightness brightness = readBrightness(in); analogWrite(colorP,brightness); Serial.println(brightness, DEC); } } // read and return the brightness from character input int readBrightness (char in) { if (in=='1' || in=='q' || in=='a'){ bright = 0; } if (in=='2' || in=='w' || in=='s'){ bright = 51; } if (in=='3' || in=='e' || in=='d'){ bright = 102; } if (in=='4' || in=='r' || in=='f'){ bright = 153; } if (in=='5' || in=='t' || in=='g'){ bright = 204; } if (in=='6' || in=='y' || in=='h'){ bright = 255; } return bright; }