Components Used
Three LEDs
Three 220-ohm resistors
Arduino board
Metal cone top to lava lamp
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
* A string of 'r' increments the red LED by 5 per 'r'
* Ditto 'g' and 'b'
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Modified 12 September 2007 by Andrew McDiarmid
*/
char serInString[10]; // array that will hold the different bytes of the string. 10=10characters;
// -> you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redValue; //These are used to track the current levels of the LEDs
int greenValue;
int blueValue;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
redValue = 127;
analogWrite(greenPin, 127); // set them all to mid brightness
greenValue = 127;
analogWrite(bluePin, 127); // set them all to mid brightness
blueValue = 127;
Serial.println("Enter color command (e.g. 'r043' or 'rrrr', max 10 characters):");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
if (serInString[1] >= 48 && serInString[1] <= 57) { //runs this code if second character is an integer character
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString + 1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
redValue = colorVal;
}
else if(colorCode == 'g') {
analogWrite(greenPin, colorVal);
greenValue = colorVal;
}
else if(colorCode == 'b'){
analogWrite(bluePin, colorVal);
blueValue = colorVal;
}
for(int i = 0 ; i <= 9; i+=1){ // indicates we've used this string and zeroes all characters in the string
serInString[i] = 0;
}
}
Serial.print(serInString);
}
else if(serInString[0] != 0){ //runs this code if the second character is not an integer character (see above)
int j = 0; //and if the first character is not the null character
while (j < 10 & serInString[j] != 0) { //iterates through the string, incrementing LEDs as indicated by the characters found
if(serInString[j] == 'r') {
redValue = (redValue + 5) % 256; //Incrementing the appropriate LED's value
analogWrite(redPin, redValue);
}
if(serInString[j] == 'g') {
greenValue = (greenValue + 5) % 256;
analogWrite(greenPin, greenValue);
}
if(serInString[j] == 'b') {
blueValue = (blueValue + 5) % 256;
analogWrite(bluePin, blueValue);
}
j++;
}
Serial.print("Red is now ");
Serial.print(redValue);
Serial.print("; green is now ");
Serial.print(greenValue);
Serial.print("; blue is now ");
Serial.print(blueValue);
Serial.println(".");
for(int i = 0 ; i <= 9; i+=1){ // indicates we've used this string and zeroes all characters in the string
serInString[i] = 0;
}
}
delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
Diffuser
I chose to reflect downwards onto a sheet of white paper. The colors mix well this way, with the exception of red, which just isn't that bright. The reflector is the top to a lava lamp.
Photos
Illuminated diffuser
Unplugged diffuser
Comments
GSI Comments
Very cool diffuser! The lamp top does a great job of reflecting the light. (I guess it's probably designed to do just that...) The legs you added also give it a nice anthropomorphic effect. I imagine that one could think of some very cool applications for your widget.
Thanks also for writing the code yourself, rather than just relying on the example we gave in class. Great way to learn how to program the Arduino!