DESCRIPTION
This lab covered digital input, output, and designing your own diffuser. I started by taking the serial input code provided by the class and editing it so that it changes the different lights depending on what the user presses. For example, the red light will increase in increments of 10% for every "r" keystroke the user enters in the serial port. Once the color brightness is maxed out, pressing the corresponding color code again brings it back to 0 and the light will turn off.
My diffuser was designed by taking strips of semi-transparent plastic that are slightly fringed at the edges. I wrapped them around a few times over in order to create a sort of "flowered" look that also gives thickness and depth to the diffuser. That way the lights from the LED bulbs will mix together better.
COMPONENTS USED
1 - Arduino Uno
1 - Breadboard
1 - Red LED
1 - Green LED
1 - Blue LED
1 - USB Cable
3 - 220 Ohm Resistors
7 - Wires
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 rColor = 0;
int gColor = 0;
int bColor = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.println("enter color command ('r', 'g', or 'b') :");
}
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' ) {
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
rColor = rColor + 25;
if(rColor > 255) {
rColor = 0;
}
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(rColor);
Serial.println();
analogWrite(redPin, rColor);
}
else if(colorCode == 'g') {
gColor = gColor + 25;
if(gColor > 255) {
gColor = 0;
}
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(gColor);
Serial.println();
analogWrite(greenPin, gColor);
}
else if(colorCode == 'b') {
bColor = bColor + 25;
if(bColor > 255) {
bColor = 0;
}
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(bColor);
Serial.println();
analogWrite(bluePin, bColor);
}
}
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