Description:
Constructed a simple circuit that activates three different colored LEDs independently. Then applied a homemade diffuser to mix the colors. Finally, adjusted the Arduino serial code so that when a user types "R", "G", or "B", it adjusts the brightness of the light up by 1/5 until it reaches full illumination.
My diffuser was a small upside-down rubbermaid tupperware container stuffed with a bit of paper towel and lined with tin foil to force the light up toward the top of the unit (the actual botton). When it didn't diffuse enough, I added a piece of the paper towel on the top to further help.
Items used for circuit:
- (1) Breadboard
- (3) 220 resistor
- (3) short connector wires
- (4) long wires
- (1) Arduino uno board
- (1) Red LED
- (1) Blue LED
- (1) Green LED
Items used for Diffuser:
- (1) Small rubbermaid tuperware container
- (1) Tin foil strip to line the edges
- (1) Piece of paper towel to cover the output area and diffuse the light
Code for incremented light based on 'r' 'g' 'b' inputs:
/*
* 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
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> 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
//set color variables to 0
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 0); // start them all to 0
analogWrite(greenPin, 0); // start them all to 0
analogWrite(bluePin, 0); // start them all to 0
Serial.println("enter 'r' 'g' or 'b' in any order to turn on lights:");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
for(int i=0;i<100;i++){
if(serInString[i]=='r') //if input in array serial array is 'r'
redColor = redColor + 51; //adds 1/5 strength to red
else if(serInString[i]=='g') //if input in array serial array is 'g'
greenColor = greenColor + 51; //adds 1/5 strength to green
else if(serInString[i]=='b') //if input in array serial array is 'b'
blueColor = blueColor + 51; //adds 1/5 strength to blue
}
//sends command to LED
analogWrite(redPin, redColor);
analogWrite(greenPin, greenColor);
analogWrite(bluePin, blueColor);
}
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++;
}
}
- Login to post comments