Use the Arduino to control a set of lights using as input the keyboard.
/*
* 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
* Command structure is "<colorCode>*X", where "colorCode" is
* one of "r","g",or "b" and "x"" is the number of times the colorCode was hitted, every keystroke represents a 10% of increment in that light
* E.g. "rr" Red =20%
* "bbb" Blue = 30%
*
* Adapted on September 13 2011, based on the code
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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 = 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 countRed = 0; //counter of number of "r's" pressed
int countGreen = 0; //counter of number of "g's" pressed
int countBlue = 0; //counter of number of "b's" pressed
int index = 0; // index to traverse the array
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
char value ;
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("enter color command (e.g. 'rrrr' (will set RED at 40%) :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
value = serInString[index];
if (value == 'r' && countRed <=10){
countRed++;
index++;
}
else if(value == 'g' && countGreen <=10){
countGreen++;
index++;
}
else if(value =='b' && countBlue <=10) {
countBlue++;
index++;
}
redValue = 25*countRed; // 25 is aprox 10% increment in brightness
greenValue = 25*countGreen;
blueValue = 25*countBlue;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
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++;
}
}