Description
The setup on the breadboard was carried over from lab (though I redid it to have a cleaner overall layout and to get the LEDs more closely clustered together). For the code I went pretty simple, going with the suggested method of counting the length of the input to determine the brightness. I went with 10% increments with a special effect (my first easter egg!) for those who attempt to set the brightness higher than 100%.
For the diffusor I think I tried at least 20 different things - lots of combinations of plastic bags of varying oppacity, plastic bottles and packaging, the cover of a Glade plug-in airfreshener, and more. I finally settled on the winning combination of a loofah under a plastic dome. [see gif here]
[Note: One small bug in my program is that very rarely the output brightness will be wrong. Based on the printed messages it appears the error is happening right after I read the input, when I use "strlen(serInString)" to count the length of the input. I'm guessing there is something about the way it does so that isn't quite what I expect and that is leading to the rare mistake in output. It happens about once every time I run the program and I can't get it to replicate with the same inputs. So if anyone knows what's up with that I'd appreciate the feedback! Thanks!]
Components
1 - Arduino Uno
1 - Breadboard
1 - Battery Pack with 2 AA batteries
3 - 220 Ω Resistors
3 - LEDs (Red, Blue, Green)
1 - White loofah
1 - Half clear plastic ball
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorValPer>", where "colorCode" is
* one of "r","g",or "b" and "colorValPer" is up to 11 of any other
* character to set the
* brightness of the LED in steps of 10% starting at 0%.
* E.g. "r" turns the red LED off.
* "g....." turns the green LED to half (50%) brightness
* "b..........." turns the blue LED UP TO ELEVEN!!!!!1
*
* Modified 23 September 2013
* by Jordan Arnesen
* Original 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 colorValPer;
int redPin = 3; // Red LED, connected to digital pin 3
int greenPin = 5; // Green LED, connected to digital pin 5
int bluePin = 6; // Blue LED, connected to digital pin 6
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. 'r....' sets red to 40%) :");
}
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' ) {
colorValPer = (strlen(serInString) -1) * 10; //counts the length of input string -1 to get % of brightness
colorVal = min(colorValPer * 2.55, 255); //converts % to appropriate value between 0-255
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(min(colorValPer, 110)); //added printline output for debugging to display the % value
Serial.print("% at "); //
Serial.print(colorVal); // in addition to the colorVal to check the internal calculations
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
if( colorValPer > 100 ) { // bonus feature for those who like to go big or go home
Serial.print(" TURNING RED UP TO ELEVEN!!!");
Serial.println(); // turning red up to 11 maxes the color and sets others to 0
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
}
}
else if(colorCode == 'b') {
analogWrite(bluePin, colorVal);
if( colorValPer > 100 ) { // bonus feature for those who like to go big or go home
Serial.print(" TURNING BLUE UP TO ELEVEN!!!");
Serial.println(); // turning blue up to 11 maxes the color and sets others to 0
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
}
}
else if(colorCode == 'g') {
analogWrite(greenPin, colorVal);
if( colorValPer > 100 ) { // bonus feature for those who like to go big or go home
Serial.print(" TURNING GREEN UP TO ELEVEN!!!");
Serial.println(); // turning green up to 11 maxes the color and sets others to 0
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
}
}
}
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