/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is a string of Multiple "r" characters or "b" or "g" characters
* Every character "r", "b", or "g" will increase the brightness by 10% roughly.
* On reaching complete or maximum brightness, it will start over from minimum.
*
* */
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
int lengthInput=0;
char colorCode;
int colorVal;
int redPin = 10; // Red LED, connected to digital pin 10
int greenPin = 11; // Green LED, connected to digital pin 11
int bluePin = 9; // Blue LED, connected to digital pin 9
int lenInput=0;
int valRed=127;
int valBlue=127;
int valGreen=127;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, valRed); // set them all to mid brightness
analogWrite(greenPin, valGreen); // set them all to mid brightness
analogWrite(bluePin, valBlue); // set them all to mid 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];
for (int j=0;j<lengthInput;j++)
{
colorCode = serInString[j];
if(colorCode == 'r' )
{
valRed=valRed+25;
if (valRed>255)
valRed=valRed-255;
analogWrite(redPin, valRed);
Serial.print(" Setting Red to ");
Serial.println(valRed);
}
else if(colorCode == 'g' )
{
valGreen=valGreen+25;
if (valGreen>255)
valGreen=valGreen-255;
analogWrite(greenPin, valGreen);
Serial.print(" Setting Green to ");
Serial.println(valGreen);
}
else if(colorCode == 'b' )
{
valBlue=valBlue+25;
if (valBlue>255)
valBlue=valBlue-255;
analogWrite(bluePin, valBlue);
Serial.print(" Setting Blue to ");
Serial.println(valBlue);
}
else
{
Serial.print(" continue to next ");
return;
}
}
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;
lengthInput=0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
lengthInput++;
}
}