Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Description
The Arduino serial monitor function uses inputs r, g, and b to control the Pulse Width Modulation and increase red, blue, and green LED lights 10% with each input. A diffuser combines the output of the three LEDs into different, modifiable colors.
Components Used
- Light Emitting Diodes (LED)--red, green, and blue
- 220 ohm resistors (3)
- Arduino board
- Bread board
- Wires
- Glass salt shaker
- Salt
- Rubber band
- Plastic bag
Arduino Code
/*
* 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
* Modified by Alison Meier, 9/12/09
*/
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 redVal;
int greenVal;
int blueVal;
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
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 mid brightness
analogWrite(greenPin, greenVal); // set them all to mid brightness
analogWrite(bluePin, blueVal); // set them all to mid brightness
Serial.println("enter character r, g, or b to increase the color brightness:");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString+1);
if(colorCode == 'r') {
redVal = (redVal + 25) % 250; //increase the level of brightness by 10% each time the character is entered until brightness reaches 100%
analogWrite(redPin, redVal);
Serial.print("setting color red to ");
Serial.print(redVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
}
else if(colorCode == 'g'){
greenVal = (greenVal + 25) % 250; //increase the level of brightness by 10% each time the character is entered until brightness reaches 100%
analogWrite(greenPin, greenVal);
Serial.print("setting color green to ");
Serial.print(greenVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
}
else if(colorCode == 'b'){
blueVal = (blueVal + 25) % 250; //increase the level of brightness by 10% each time the character is entered until brightness reaches 100%
analogWrite(bluePin, blueVal);
Serial.print("setting color blue to ");
Serial.print(blueVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
}
}
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++;
}
}
Item
Arduino board with 3 LEDsBoard with salt shaker diffuser, lights on
Salt shaker diffuser, lights off, photo 1
Salt shaker diffuser, lights off, photo 2