L2 - Digital I/O
Description
Use the Arduino serial communication to control three light emitting diodes (red, green, and blue) via keyboard commands.
Components Used
- Light Emitting Diodes (LEDs)
- Resistors (220 ohms)
- Diffuser
Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is a series of 'r's, 'g's, or 'b's where the number of times
* the color code occurs indicates by how much to increase or decrease the brightness,
* in increments of 10%.
* E.g. "rrrrr" increase (or decrease) red LED brightness by 50% of max brightness
* "ggg" increase (or decrease) green LED brightness by 30% of max brightness
*
* Created 3 February 2011
*/
static int maxValue = 255;
static int minValue = 0;
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 bluePin = 10; // Blue LED, connected to digital pin 10
int greenPin = 11; // Green LED, connected to digital pin
// Initial values are 0
int redVal = 0;
int blueVal = 0;
int greenVal = 0;
float fadeVal = 25.5
;
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 off (0)
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Serial.println("enter color command (e.g. 'rrr') :");
}
int setNewValue(int pinVal, int stringLength) {
int newVal = pinVal + stringLength * fadeVal;
// make sure new value is within min/max brightness range
if (newVal >= maxValue) {
newVal = maxValue;
fadeVal = -fadeVal;
} else if (newVal <= minValue) {
newVal = minValue;
fadeVal = -fadeVal;
}
return newVal;
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
int stringLength = readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
boolean validCommand = true;
//verify that the command is valid
for (int i = 1; i < stringLength; i++) {
if (serInString[i] != colorCode) {
validCommand = false;
break;
}
}
if (validCommand) {
if(colorCode == 'r') {
Serial.print('r');
redVal = setNewValue(redVal, stringLength);
Serial.print(redVal);
Serial.println();
analogWrite(redPin, redVal);
}
else if(colorCode == 'g') {
Serial.print('g');
greenVal = setNewValue(greenVal, stringLength);
Serial.print(greenVal);
Serial.println();
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'b') {
Serial.print('b');
blueVal = setNewValue(blueVal, stringLength);
Serial.print(blueVal);
Serial.println();
analogWrite(bluePin, blueVal);
}
}
else {
Serial.println("invalid command.");
}
}
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
//returns the length of the serial string
int readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return 0
;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
return i;
}