Lab2 - LED Diffuser
Description
Use a cotton with IKEA polyester box to diffuse the light. Can be used as a night lamp.
1. Starts with the sample fading effect.
2. Part 2 is multiple key press. The user can input any sequence and amount of character 'r', 'g', 'b' or 'R', 'G', 'B'; every character adds the brightness by 10% (+25.5) of that color. For example, 'rrgbrbrgg' increased red by 40%, green 30%, and blue 20%.
3. Part 3 let user inputs a HEX code of color starts with the character '#'. For example if input '#FF8C00', the color becomes orange. (RGB-HEX conversion can be found here.)
Components Used
1- Arduino Uno
1- Breadboard
3- LED (R,G,B)
3- 220Ω Resistor
1- cutton
1- IKEA polyester box
Code
/*
* Add HEX
* ---------------
* Sample input: #FF00FF
*
* Created 6 Feb 2013
* Kate Hsiao
*/
char serInString[100];
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
// For demo use
int demo = 1;
int rFadeVal = 255;
int gFadeVal = 1;
int bFadeVal = 1;
int c = 0;
// For multiple key use
double rVal = 0;
double gVal = 0;
double bVal = 0;
// For Hex Use
int hexArray[] = {0,0,0,0,0,0};
int caseOffset = 0;
//int rHex = 0;
//int gHex = 0;
//int bHex = 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 Hex Color starts with '#' (e.g. '#FF0000'), or multiple 'rgb' (e.g. 'bbb') :");
}
void loop () {
/* Automatical Fading Part */
/* (just for a nice demo start) */
if (demo) {
c += 1; // Increment counter
if (c < 255) // First phase of fades
{
rFadeVal -= 1; // Red down
gFadeVal += 1; // Green up
bFadeVal = 1; // Blue low
}
else if (c < 509) // Second phase of fades
{
rFadeVal = 1; // Red low
gFadeVal -= 1; // Green down
bFadeVal += 1; // Blue up
}
else if (c < 763) // Third phase of fades
{
rFadeVal += 1; // Red up
gFadeVal = 1; // Green low
bFadeVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
c = 1;
}
analogWrite(redPin, rFadeVal); // Write current values to LED pins
analogWrite(greenPin, gFadeVal);
analogWrite(bluePin, bFadeVal);
}
/* Assignment Part */
memset(serInString, 0, 100); // clear the string
readSerialString(serInString);
colorCode = serInString[0];
/* Part 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);
}
*/
// Part 2. Multiple Key Press
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' || colorCode == 'R' || colorCode == 'G' || colorCode == 'B' ) {
demo = 0; // toggle off demo effect
int i=0;
while (serInString[i]!='\0') {
switch (serInString[i]) {
case 'r':
case 'R':
rVal = rVal + 25.5;
if (rVal>255) {
rVal -= 255;
}
break;
case 'g':
case 'G':
gVal = gVal + 25.5;
if (gVal>255) {
gVal -= 255;
}
break;
case 'b':
case 'B':
bVal = bVal + 25.5;
if (bVal>255) {
bVal -= 255;
}
break;
default:
Serial.print("Wrong Input.");
Serial.println();
break;
}
i++;
}
analogWrite(redPin, rVal);
analogWrite(greenPin, gVal);
analogWrite(bluePin, bVal);
Serial.print("Setting color to (");
Serial.print(rVal);
Serial.print(",");
Serial.print(gVal);
Serial.print(",");
Serial.print(bVal);
Serial.print(")");
Serial.println();
}
// Part 3. Get Input for HEX Value
if ( colorCode =='#' ) {
demo = 0; // toggle off demo effect
serInString[0] = 0;
for (int k=1; k<7; k++) {
if( serInString[k]>47 && serInString[k]<58 ) { // 0-9
caseOffset = 48;
}
else if ( serInString[k]>64 && serInString[k]<71 ) { // A-F
caseOffset = 55;
}
else if ( serInString[k]>96 && serInString[k]<103 ) { // a-f
caseOffset = 87;
}
else { // wrong
caseOffset = 0;
Serial.print("Wrong Input.");
Serial.println();
return;
}
hexArray[k-1] = serInString[k] - caseOffset;
}
//rHex = hexArray[0]*16 + hexArray[1];
//gHex = hexArray[2]*16 + hexArray[3];
//bHex = hexArray[4]*16 + hexArray[5];
rVal = hexArray[0]*16 + hexArray[1];
gVal = hexArray[2]*16 + hexArray[3];
bVal = hexArray[4]*16 + hexArray[5];
//analogWrite(redPin, rHex);
//analogWrite(greenPin, gHex);
//analogWrite(bluePin, bHex);
analogWrite(redPin, rVal);
analogWrite(greenPin, gVal);
analogWrite(bluePin, bVal);
Serial.print("Setting color to (");
Serial.print(rVal);
Serial.print(",");
Serial.print(gVal);
Serial.print(",");
Serial.print(bVal);
Serial.print(")");
Serial.println();
}
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++;
}
}
Video
Please see the video for partial demo.
- Login to post comments
Drupal theme by Kiwi Themes.