Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
&quo
Description
This program controls each color LED with their corresponding letter: eg. R for red, G for Green and B for Blue. Use the upper case of these letters to make them brighter and the lowercase to make them dimmer: eg. R for brighter and r for dimmer. Enter multiple letters to cause multiple changes: eg. RRGGBB for twice as bright.
Components Used
- 3 Light Emitting Diode (LED)
- 3 Resistors
- Arduino Board
- Breadboard
- Tea Filter
- Fluff
Arduino Code
/* LED Letter Control
*
* This program controls 3 LED lights by brightening
* and dimming with letters on the keyboard.
*
* Command structure is "<colorCode>*", where "colorCode" is
* one of "R", "r", "G","g","B", or "b".
* E.g. "R" increases the red LED brightness by 25
* "gg" decreases the red LED brightness by 50
* "RGB" increases the red, green and blue LEDs by 25 each
*
* Created September 16th, 2009
* George Hayes <ghayes@ischool.berkeley.edu
*
*/
//include support for manipulating strings.
//for a useful string comparison function, see the bottom of this file... stringsEqual()
//#include <stdio.h>
#include <string.h>
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;
t;>
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 = 25;
int greenValue = 25;
int blueValue = 25;
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
Serial.println("Control each LEDs color with their corresponding letter.");
Serial.println("eg. R for red, G for Green and B for Blue.");
Serial.println("");
Serial.println("Use the uppercase of these letters to make them brighter");
Serial.println("and the lowercase to make them dimmer:");
: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-size: 11px; font-family: inherit; vertical-align: baseline; padding: 0px; border: 0px initial initial;">Serial.println("eg. R for brighter red and r for dimmer red.");
Serial.println("");
Serial.println("Enter multiple letters to cause multuple changes:");
Serial.println("eg. RRGGBB for twice as bright.");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
//Read commands of the form 'rrrb'
processRepeatKeyCommands(serInString, 100);
//next loop
resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray, int maxLength) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() && i < maxLength) {
strArray[i] = Serial.read();
i++;
}
}
//go through the string, and increase the red value for each 'R',
//the green value for each 'G', and the blue value for each 'B'.
//use the lower case r, g and b to dim the LEDs
//For example "RRGG" increases red by 30 and green by 10.
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)...
order: 0px initial initial;">if (colorCode == 'R') {
//Increase the current red value by 25, and if you reach 255 go back to 0
redValue = (redValue + 25) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color R to ");
Serial.println(redValue);
} else if (colorCode == 'r') {
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 = (greenValue + 25) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color G to ");
Serial.println(greenValue);
} else if (colorCode == 'g') {
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 = (blueValue + 25) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color B to ");
Serial.println(blueValue);
} else if (colorCode == 'b') {
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++;
}
}
Images