/*
* Serial RGB LED Modified
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorInc>", where "colorCode" is
* one of "r","g",or "b" and "colorInc" is +, -, ++, or --.
* E.g. "r+" increments the red LED +20%.
* "g-" decrements the green LED -20%
* "b++" turns the blue LED to full brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
* Modified 11 September 2011 by Justin Wang
*/
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;
char colorInc;
char colorSwitch;
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
int redVal = 102;
int greenVal = 102;
int blueVal = 102;
int redPercent = 40;
int greenPercent = 40;
int bluePercent = 40;
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 mid brightness
analogWrite(greenPin, greenVal); // set them all to mid brightness
analogWrite(bluePin, blueVal); // set them all to mid brightness
Serial.println("Enter color command (e.g. 'r+, G-, b++'):");
}
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];
colorInc = serInString[1];
colorSwitch = serInString[2];
// make work for capital color codes
if (colorCode=='R')
{
colorCode='r';
}
else if (colorCode=='G')
{
colorCode='g';
}
else if (colorCode=='B')
{
colorCode='b';
}
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' && colorInc == '+' || colorInc == '-')
{
switch (colorCode) {
case 'r':
if (colorInc == '+' && colorSwitch == '+')
{
redVal = 255; // 100%
}
else if (colorInc == '-' && colorSwitch == '-')
{
redVal = 0; // 0%
}
else if (colorInc == '+')
{
if (redVal!=255)
{
redVal = redVal+51; // +20%
}
}
else if (colorInc == '-' )
{
if (redVal!=0) // if not already 0%
{
redVal = redVal-51; // -20%
}
}
analogWrite(redPin, redVal); // set LED value
break;
case 'g':
if (colorInc == '+' && colorSwitch == '+')
{
greenVal = 255; // 100%
}
else if (colorInc == '-' && colorSwitch == '-')
{
greenVal = 0; // 0%
}
else if (colorInc == '+')
{
if (greenVal!=255)
{
greenVal = greenVal+51; // +20%
}
}
else if (colorInc == '-' )
{
if (greenVal!=0) // if not already 0%
{
greenVal = greenVal-51; // -20%
}
}
analogWrite(greenPin, greenVal); // set LED value
break;
case 'b':
if (colorInc == '+' && colorSwitch == '+')
{
blueVal = 255; // 100%
}
else if (colorInc == '-' && colorSwitch == '-')
{
blueVal = 0; // 0%
}
else if (colorInc == '+')
{
if (blueVal!=255)
{
blueVal = blueVal+51; // +20%
}
}
else if (colorInc == '-' )
{
if (blueVal!=0) // if not already 0%
{
blueVal = blueVal-51; // -20%
}
}
analogWrite(bluePin, blueVal); // set LED value
break;
}
// convert LED values to percentage
switch (redVal) {
case 255:
redPercent = 100;
break;
case 204:
redPercent = 80;
break;
case 153:
redPercent = 60;
break;
case 102:
redPercent = 40;
break;
case 51:
redPercent = 20;
break;
case 0:
redPercent = 0;
break;
}
switch (greenVal) {
case 255:
greenPercent = 100;
break;
case 204:
greenPercent = 80;
break;
case 153:
greenPercent = 60;
break;
case 102:
greenPercent = 40;
break;
case 51:
greenPercent = 20;
break;
case 0:
greenPercent = 0;
break;
}
switch (blueVal) {
case 255:
bluePercent = 100;
break;
case 204:
bluePercent = 80;
break;
case 153:
bluePercent = 60;
break;
case 102:
bluePercent = 40;
break;
case 51:
bluePercent = 20;
break;
case 0:
bluePercent = 0;
break;
}
// print LED state
Serial.println();
Serial.print("R: ");
Serial.print(redPercent);
Serial.println("%");
Serial.print("G: ");
Serial.print(greenPercent);
Serial.println("%");
Serial.print("B: ");
Serial.print(bluePercent);
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++;
}
}