Lab 2: Digital I/O with Arduino Boards + Diffuser
Description
Use Arduino to control three LEDs at once, starting with automatically fading through each LED and eventually using the serial monitor to control the LEDs.
Components Used
- 3 LEDs: red, green, blue
- 3 220ohm resistors
- tissue and old film canister used as diffuser
Code
My code allows three input options which is set by the variable commandType. Setting commandType to 0 allows the numeric 'r43', 1 allows repeating letters 'rrrggb', and 2 allows some preset color options.
/*
* 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://tod
*/
//Set commandType to:
// 0 for numeric (e.g. 'r43')
// 1 for repeating letters (e.g. 'rrrggb')
// 2 for preset colors (e.g. 'r' for red, 'v' for violet)
int commandType = 2;
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 redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); // set them all to mid brightness
analogWrite(greenPin, greenValue); // set them all to mid brightness
analogWrite(bluePin, blueValue); // set them all to mid brightness
if(commandType == 0){
Serial.println("enter color command (e.g. 'r43') :");
} else if (commandType == 1){
Serial.println("enter repeating letters command (e.g. 'rrrggb') :");
} else if (commandType == 2){
Serial.println("enter color command, options are: r o y g b i v :");
} else {
Serial.println("please select a valid command type");
}
}
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(commandType == 0){
processNumericCommands(serInString);
}else if (commandType == 1){
processRepeatKeyCommands(serInString, 100);
}else if (commandType == 2){
processRoygbiv(serInString);
}
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++;
}
}
void processNumericCommands(char *strArray){
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
}
void processRepeatKeyCommands(char *strArray, int maxLength) {
int i = 0;
//loop through the string (strArray)
//i = the current position in the string
//Stop when either (a) i reaches the end of the string or
// (b) there is an empty character '\0' in the string
while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];
//If the character is r (red)...
if (colorCode == 'r') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = 0;
redValue = (redValue + 25) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = 0;
greenValue = (greenValue + 25) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = 0;
blueValue = (blueValue + 25) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
}
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}
void processRoygbiv(char *strArray) {
colorCode = serInString[0];
//red
if (colorCode == 'r') {
redValue = 255;
greenValue = 0;
blueValue = 0;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to red");
//orange
} else if (colorCode == 'o'){
redValue = 255;
greenValue = 165;
blueValue = 0;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to orange");
//yellow
} else if (colorCode == 'y'){
redValue = 255;
greenValue = 255;
blueValue = 0;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to yellow");
//green
} else if (colorCode == 'g'){
redValue = 0;
greenValue = 255;
blueValue = 0;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to green");
//blue
} else if (colorCode == 'b'){
redValue = 0;
greenValue = 0;
blueValue = 255;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to blue");
//indigo
} else if (colorCode == 'i'){
redValue = 75;
greenValue = 0;
blueValue = 130;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to indigo");
//violet
} else if (colorCode == 'v'){
redValue = 238;
greenValue = 130;
blueValue = 238;
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("setting color to violet");
}
}