/*
* Serial RGB LED -- Modified by Shaun Giudici
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Script will count the occurrances of R's G's and B's
* Each occurance will count for 20% brightness
*
* Script will also count nmuber of F's
* More F's == More Speed
*
* For example the command "rrrgbfff" will set red led to 60%, green led to 20% and blue led to 20% at medium speed
*
*/
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 redPin =11; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 9; // Blue LED, connected to digital pin 11
int r =0;
int g =0;
int b =0;
int f =0; // Start with fading off
int fadeVals[3];
int fadeSpeed = 10; // default fade speed
int fadeDirection = 0; // 0 means fade out
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("Each R, G, or B gets you 20% LED brightness");
Serial.println("Enter F's to change fade speed");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
if (serInString[0] != 0) {
r=0;
g=0;
b=0;
f=0;
for (int i=0; i<100; i++) {
if(serInString[i] == 'r' || serInString[i] == 'R') {
r++;
}
else if(serInString[i] == 'g' || serInString[i] == 'G') {
g++;
}
else if(serInString[i] == 'b' || serInString[i] == 'B') {
b++;
}
else if(serInString[i] == 'f' || serInString[i] == 'F') {
f++;
}
}
r=255*r/5;
g=255*g/5;
b=255*b/5;
if(r>255){r=255;}
if(g>255){g=255;}
if(b>255){b=255;}
Serial.print("setting RGB to (");
Serial.print(r);
Serial.print(", ");
Serial.print(g);
Serial.print(", ");
Serial.print(b);
Serial.print(")");
Serial.println();
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
// set fading
fadeSpeed = 200/f;
fadeVals[0] = r;
fadeVals[1] = g;
fadeVals[2] = b;
}
if (f>0) {
Serial.print(fadeVals[0]);
Serial.print(" ");
Serial.print(fadeVals[1]);
Serial.print(" ");
Serial.print(fadeVals[2]);
Serial.println();
if (!fadeDirection) {
if (fadeVals[0] > 0) {fadeVals[0]--; }
if (fadeVals[1] > 0) {fadeVals[1]--; }
if (fadeVals[2] > 0) {fadeVals[2]--; }
if (fadeVals[0] == 0 && fadeVals[1] == 0 && fadeVals[2] == 0) { fadeDirection = 1; }
} else {
if (fadeVals[0] < r) {fadeVals[0]++; }
if (fadeVals[1] < g) {fadeVals[1]++; }
if (fadeVals[2] < b) {fadeVals[2]++; }
if (fadeVals[0] == r && fadeVals[1] == g && fadeVals[2] == b) { fadeDirection = 0; }
}
analogWrite(redPin, fadeVals[0]); // Write current values to LED pins
analogWrite(greenPin, fadeVals[1]);
analogWrite(bluePin, fadeVals[2]);
delay(fadeSpeed);
} else {
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