a plastic bottle rim.
/*
* 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/
* Combined and Adapted 2011 by Timothy Charoenying
*/
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 redness=0;
int greenness=0;
int blueness=0;
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 25); // set them all to 10% brightness
analogWrite(greenPin, 25); // set them all to 10% brightness
analogWrite(bluePin, 25); // set them all to 10% brightness
Serial.println("to toggle the redness, greenness, or blueness by a factor of 10%, enter R, G, or B, respectively:");
}
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' || colorCode == 'o' || colorCode == 'p' || colorCode == 'm')
{
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
redness = redness + 25;
analogWrite(redPin, redness);
}
else if(colorCode == 'g') {
greenness = greenness + 25;
analogWrite(greenPin, greenness);
}
else if(colorCode == 'b') {
blueness = blueness + 25;
analogWrite(bluePin, blueness);
}
else if(colorCode == 'o') { // for orangish color
analogWrite(redPin, 179);
analogWrite(bluePin, 0);
analogWrite(greenPin, 127);
}
else if(colorCode == 'p') { // for purplish color
analogWrite(redPin, 155);
analogWrite(bluePin, 155);
analogWrite(greenPin, 0);
}
else if(colorCode == 'm') { // for maximum color
analogWrite(redPin, 255);
analogWrite(bluePin, 255);
analogWrite(greenPin, 255);
}
}
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++;
}
}