Description
For the diffuser design, I decide to use Scotch tape to wrap around the light and create a cone shape (at least what I was aiming for). I was looking for a material that's not totally transparent, which won't have the diffusing effect, but transparent enough so that the low-powered LED lights can shine through. So Scotch tape seems like a perfect candidate. I experiemented with it and it worked.
For the optional project, I decide to turn keyboard into a slider that controls the brightness of each light. So each slider consists three keys on the keyboard and are covered with a slider sticker indicating usage. The more you slide along the stick means more buttons are being pressed and I use this information to program the light brightness. And if you slide backwards, the light will dim accordingly. One problem I haven't solved yet is that if the light is already at its brightest, further brightening it brings it to zero brightness.
Components Used
1- Arduino Uno
3- 22 Ω Resistor
1- Breadboard
3- LED Lights
Code
Problem 2
/*
* 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 = 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, 1); // set them all to low brightness
analogWrite(greenPin, 1); // set them all to low brightness
analogWrite(bluePin, 1); // set them all to low brightness
Serial.println("enter color command (e.g. 'rrr') :");
}
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' ) {
int brightness = 0;
while (serInString[brightness+1] == 'r' || serInString[brightness+1] == 'g' || serInString[brightness+1] == 'b') {
brightness++;
}
colorVal = 25 * (brightness+1) + 5;
Serial.print("brighting light ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(brightness+1);
Serial.print("0%");
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++;
}
}
Problem 3
/*
* 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 = 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
int rVal = 1;
int gVal = 1;
int bVal = 1;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 1); // set them all to low brightness
analogWrite(greenPin, 1); // set them all to low brightness
analogWrite(bluePin, 1); // set them all to low brightness
Serial.println("slide to change the brightness");
}
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' ) {
int brightness = 0;
int colorMod = 0;
int counter = 0;
while (serInString[counter] != '\0') {
counter++;
}
brightness = counter - 1;
colorMod = 51 * (brightness);
Serial.print("brighting light");
Serial.print(" by ");
Serial.print(brightness);
Serial.print("0%");
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
rVal = rVal + colorMod;
analogWrite(redPin, rVal);
}
else if(colorCode == 'g') {
gVal = gVal + colorMod;
analogWrite(greenPin, gVal);
}
else if(colorCode == 'b') {
bVal = bVal + colorMod;
analogWrite(bluePin, bVal);
}
}
if( colorCode == 'y' || colorCode == 'j' || colorCode == 'm' ) {
int brightness;
int colorMod;
int counter = 0;
while (serInString[counter] != '\0') {
counter++;
}
brightness = counter - 1;
colorMod = 51 * (brightness);
Serial.print("dimming light");
Serial.print(" by ");
Serial.print(brightness);
Serial.print("0%");
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'y') {
rVal = rVal - colorMod;
analogWrite(redPin, rVal);
}
else if(colorCode == 'j') {
gVal = gVal - colorMod;
analogWrite(greenPin, gVal);
}
else if(colorCode == 'm') {
bVal = bVal - colorMod;
analogWrite(bluePin, bVal);
}
}
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