The Goal:
The goal was to allow users to control the brightness of LEDs through inpuut device, and diffuse the light in an interesting way. I created a diffuser out of plastic cups, paper and transparent gel balls used for decorating plants. The contraption diffuses the mix of colors well and reflects multiple times creating a nice effect. I modified the input to allow users to even decrease the brightness as needed.
Components:
1 Breadboard
1 Arduino
3 LEDs (Red, Blue, Green)
1 USB Cable
3 220 Ohm Resistors
4 wires
The 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
*
* 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 redVal;
int greenVal;
int blueVal;
char sign;
int redPin = 10; // Red LED, connected to digital pin 9
int greenPin = 11; // Green LED, connected to digital pin 10
int 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, 50); // set them all to mid brightness
analogWrite(greenPin, 50); // set them all to mid brightness
analogWrite(bluePin, 50); // set them all to mid brightness
Serial.print("Enter r, g or b increase brightness; R, G, or B to decrease brightness : ");
Serial.println();
}
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];
Serial.print(colorCode);
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' || colorCode == 'R' || colorCode == 'G' || colorCode == 'B') {
Serial.print("Enter r, g or b decrease brightness; R, G, or B to increase brightness : ");
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'R'){
redVal = (redVal+25)%250; // increase by 25 and when it reaches beyong 250, bring it back to zero
analogWrite(redPin, redVal);
}
else if(colorCode == 'r'){
redVal = (redVal-25);
if (redVal < 0){ // while decreasing, if value goes below 0, reset it to 0
redVal = 0;
}
analogWrite(redPin, redVal);
}
else if(colorCode == 'G'){
greenVal = (greenVal+25)%250;
analogWrite(greenPin, greenVal);
}
else if (sign == 'g'){
greenVal = (greenVal-25);
if (greenVal < 0){
greenVal = 0;
}
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'B'){
blueVal = (blueVal+25)%250;
analogWrite(bluePin, blueVal);
}
else if (sign == 'b'){
blueVal = (blueVal-25);
if (blueVal < 0){
blueVal = 0;
}
analogWrite(bluePin, blueVal);
}
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