/*
* 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/
*
* John utilized the code and changed it 09/13/2011
* it is buggy but it works!
*/
//------------------------------------------------------------------------------------------
char serInString[100];
//int photocellPin = 2; // the cell and 10K pulldown are connected to a0
//int photocellReading; // the analog reading from the sensor divider
int LEDpinRed = 3; // Red LED
int LEDpinGreen = 6; // Green LED
int LEDpinBlue = 5; // Blue LED
//int LEDbrightness; //the value given by the PhotoCell
int mySpeed = 100; //the update speed
int myCounter = 0;
int colorCode;
unsigned int LEDredValue = 220;
unsigned int LEDgreenValue = 10;
unsigned int LEDblueValue = 10;
//------------------------------------------------------------------------------------------
//
void setup() {
pinMode(LEDpinRed, OUTPUT);
pinMode(LEDpinGreen, OUTPUT);
pinMode(LEDpinBlue, OUTPUT);
Serial.begin(9600);
analogWrite(LEDpinRed, LEDredValue);
analogWrite(LEDpinGreen, LEDgreenValue);
analogWrite(LEDpinBlue, LEDblueValue);
}
//------------------------------------------------------------------------------------------
//
void loop () {
memset(serInString, 0, 100); // clear the string
readSerialString(serInString); //read the serial port and create a string out of what you read
myInput(serInString, 100);
readSerialString(serInString);
delay(mySpeed); //update speed
//------------------------------------------------------------------------------------------
// PHOTOCELL
// photocellReading = analogRead(photocellPin);
// Serial.println(photocellReading); // the raw analog reading
// photocellReading = photocellReading; //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
// LEDbrightness = map(photocellReading, 0, 1023, 0, 255); //added this to give output of photocell value
}
//------------------------------------------------------------------------------------------
// ANALYZING THE INPUT
void myInput(char *strArray, int length) {
while (myCounter < length && strArray[myCounter] != '\0') {
colorCode = strArray[myCounter];
if(colorCode == 'r') {
LEDredValue = (LEDredValue + 10);
Serial.print("Red Value: ");
Serial.println(LEDredValue);
analogWrite(LEDpinRed, LEDredValue);
}
else if(colorCode == 'g') {
LEDgreenValue = (LEDgreenValue + 10);
Serial.print("Green Value: ");
Serial.println(LEDgreenValue);
analogWrite(LEDpinGreen, LEDgreenValue);
}
else if(colorCode == 'b') {
LEDblueValue = (LEDblueValue + 10);
Serial.print("Blue Value: ");
Serial.println(LEDblueValue);
analogWrite(LEDpinBlue, LEDblueValue);
}
++myCounter;
}
}
//------------------------------------------------------------------------------------------
// READ STRING AND STORE IN AN ARRAY
//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++;
}
} //end
//------------------------------------------------------------------------------------------