DESCRIPTION
To build my diffuser, I used a paper towel roll and covered the top with bubble wrap to give a better light dispersal. I tried cutting the paper towel roll in half to make the diffuser shorter and more convenient to transport, but realized that the longer roll worked better as a diffuser because the blend of the LED colors seemed more uniform so I reassembled the paper towel roll into its original, longer shape.
After I built my diffuser, I modified the serial_led_rgb code which was provided in class so that the RGB LEDs all started at 0% brightness. Then, inputting an "r", "g" or "b" as a serial command would increase the corresponding LED's brightness by about 10% (I rounded my brightness step size to 25 instead of 25.5, so it wasn't exact). Once the LED brightness reached about 100% (brightness equals 250 instead of 255), the next input would reset the LED to 0% brightness or "off."
I used the modified serial_led_rgb code to find the appropriate LED brightnesses to create the different colors of the rainbow. Then, I wrote a "Rainbow Diffuser" code in which you input the first letter of a color of the rainbow as a serial command. Then, the LEDs light to their appropriate brightnesses such that the diffuser will show that color. For example, if "o" is input, the red and green LEDs light to their appropriate brightnesses such that the diffuser will give off an orange light.
COMPONENTS USED
1- Arduino Uno (with laptop and USB cable)
3- 220 Ω Resistors
1- Breadboard
1- Red LED
1- Green LED
1- Blue LED
22-gauge wire
1 - paper towel roll
1 - 6"x6" piece of bubble wrap
CODE #1
/*
* Modified Serial RGB LED
* ---------------
* Serial commands increase the brightness of R,G,B LEDs by 10% until 100% is reached, then the subsequent input returns the corresponding LED to "off"
*
* Command structure is "colorCode", where "colorCode" is either "r","g",or "b"
* E.g. "r" increases the red LED brightness by 10%.
* "g" increases the green LED brightness by 10%
* "b" increases the blue LED brightness by 10%
*
* Created 23 September 2013
* Base code, which was modified for assignment, borrowed from:
* 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 blueVal;
int greenVal;
int redPin = 10; // Red LED, connected to digital pin 10
int greenPin = 9; // Green LED, connected to digital pin 9
int bluePin = 11; // 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, redVal); // set them all to 0 brightness
analogWrite(greenPin, greenVal); // set them all to 0 brightness
analogWrite(bluePin, blueVal); // set them all to 0 brightness
Serial.println("press 'r', 'g', or 'b' to make that color LED 10% brighter (e.g. 'red'). Once an LED value reaches 250, brightness will reset to zero if color is input again");
}
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];
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
redVal = (redVal + 25)%275; // increase brightness by 25 until value reaches 250, then reset to zero
Serial.print("setting red LED to ");
Serial.print(redVal);
Serial.println();
analogWrite(redPin, redVal);
}
else if(colorCode == 'g') {
greenVal = (greenVal + 25)%275; // increase brightness by 25 until value reaches 250, then reset to zero
Serial.print("setting green LED to ");
Serial.print(greenVal);
Serial.println();
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'b') {
blueVal = (blueVal + 25)%275; // increase brightness by 25 until value reaches 250, then reset to zero
Serial.print("setting blue LED to ");
Serial.print(blueVal);
Serial.println();
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++;
}
}
CODE #2 (OPTIONAL)
/*
* Rainbow Diffuser
* ---------------
* Serial commands control the output color of the diffuser
*
* Command structure is "<colorCode>", where "colorCode" is
* the first letter of a color of the rainbow.
* E.g. "r" turns the red LED on so the diffuser shows red.
* "o" turns the red and green LEDs to certain brightnesses to show orange
*
* Created 23 September 2013
* Main code (but with personal modifications) borrowed from:
* 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 blueVal;
int greenVal;
int redPin = 10; // Red LED, connected to digital pin 10
int greenPin = 9; // Green LED, connected to digital pin 9
int bluePin = 11; // 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, redVal); // set them all to 0 brightness
analogWrite(greenPin, greenVal); // set them all to 0 brightness
analogWrite(bluePin, blueVal); // set them all to 0 brightness
Serial.println("Input the first letter of a color of the rainbow (e.g. 'r' for red, 'o' for orange, etc.");
}
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];
serInString[0] = 0;
if(colorCode == 'r') {
redVal = 250; //set red LED on
blueVal = 0; //turn blue LED off
greenVal = 0; //turn green LED off
Serial.print("setting LEDs so the diffuser outputs red");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'o') {
redVal = 255; //set red LED on
blueVal = 0; //set blue LED off
greenVal = 25; //set green LED on
Serial.print("setting LEDs so the diffuser outputs orange");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'y') {
redVal = 250; //set red LED on
blueVal = 0; //set blue LED off
greenVal = 50; //set green LED on
Serial.print("setting LEDs so the diffuser outputs yellow");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'g') {
redVal = 0; //set red LED off
blueVal = 0; //set blue LED off
greenVal = 255; //set green LED on
Serial.print("setting LEDs so the diffuser outputs green");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'b') {
redVal = 0; //set red LED off
blueVal = 255; //set blue LED on
greenVal = 0; //set green LED off
Serial.print("setting LEDs so the diffuser outputs blue");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'p') {
redVal = 250; //set red LED on
blueVal = 250; //set blue LED on
greenVal = 0; //set green LED off
Serial.print("setting LEDs so the diffuser outputs purple");
Serial.println();
analogWrite(redPin, redVal);
analogWrite(bluePin, blueVal);
analogWrite(greenPin, greenVal);
}
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++;
}
}
IMAGES
Note: the diffuser is more prominent at night in the dark.
- Login to post comments