DESCRIPTION
The objective of this assignment was to create a system that would light the Red, Green, and Blue LEDs and make them fade in and out. This was done by setting the LEDs up correctly on the Arduino board and loading the pre-created code available online. Next, I chose to diffuse the light using a foam strip to first diffuse the harsh light and to give it more room to diffuse. I then a cut out ping-pong ball to further diffuse the light and allow the ball to appear nice and evenly lit. In the attached picture, the foam covers the LED lights. In order to give a stronger and more even glow, I put 3 LEDs of each color in parallel.
The next task was modify the existing code so that each color of LED would change depending on the input. In this case, I chose to create the system so that each 'r', 'g', and 'b' typed would power the LED by 10%. Two r's would give a brightness of 20% for the red LED. I essentially created the code so that it counts the number of r's, g's, and b's are put in, into the input and correspondingly sets the brightness of each LED at how many each letter was typed.
COMPONENTS USED
1 Breadboard
1 Arduino Uno
3 Red LEDs
3 Green LEDs
3 Blue LEDs
1 USB cable
3 220 ohm resistors
10 wires
1 piece of foam
1 ping-pong ball
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 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 r = 0;
int g = 0;
int b = 0;
int inputExists = 0;
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
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'rrrrgb') :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
int counter = 0;
colorCode = serInString[counter];
while (colorCode == 'r' || colorCode == 'g' || colorCode == 'b') {
if (inputExists == 0) {
r = 0;
g = 0;
b = 0;
}
inputExists = 1;
if (colorCode == 'r') {
r += 1;
}
else if (colorCode == 'g') {
g += 1;
}
else if (colorCode == 'b') {
b += 1;
}
counter += 1;
colorCode = serInString[counter];
}
if ( r <= 10 ) { r = 25*r; }
else { r = 255; }
if ( g <= 10 ) { g = 25*g; }
else { g = 255; }
if ( b <= 10 ) { b = 25*b; }
else { b = 255; }
/*
Serial.print(' ');
Serial.print(r);
Serial.print(g);
Serial.print(b);
Serial.print(' ');
*/
if (inputExists == 1) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
inputExists = 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();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
*/
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