Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
<!-- header starts-->
<!-- main --> Digital I/O and Diffuser Lab
Description
- First, I added two additional LEDs and then made their brightnesses appear to fade
- With the LEDs in serial, a program was uploaded that allowed me to enter brightness values
- I created a diffuser out of a white candle
- I wrote code so that i was able to enter between 1 and 10 r, g, or bs and translated them to a color value between 0 and 255
Components Used
- Breadboard
- White wax candle
- 3 Light Emitting Diode (LED)
- Resistor
- Rubber bands
Arduino Code
/*
* Percentge 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" is equivalent to a 10% change in brightness. rrr would be approximately a value of 76.
*
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Modified September 16 2009
* by Heather Dolan
* for a TUI lab
*/
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 numLetter;
int x;
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() {
//int x = 0;
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 5); // set them all to mid brightness
analogWrite(greenPin, 5); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r' or 'rrr', etc. One letter is nearly equivalent to a 10% increment) :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
Serial.print(serInString[0]);
x = 0;
numLetter = 0;
colorCode = serInString[0];
while(serInString[x]!=false) {
if(colorCode == 'r' || colorCode == 'g' || colorCode == 'b') {
numLetter = 1 + numLetter; //increment the counter for the number letters entered
serInString[x]=0; //clear the string
}
x++;
}
//set the color and output the value
colorVal = getColorValue(numLetter);
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
Serial.print("Red Color Value equals: ");
Serial.print(colorVal);
Serial.println();
}
else if(colorCode == 'g'){
analogWrite(greenPin, colorVal);
Serial.print("Green Color Value equals: ");
Serial.print(colorVal);
Serial.println();
}
else if(colorCode == 'b'){
analogWrite(bluePin, colorVal);
Serial.print("Blue Color Value equals: ");
Serial.print(colorVal);
Serial.println();
}
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++;
}
}
//Function for converting the number of letters entered to an 8-bit color value
int getColorValue (int nL) {
int cV;
cV = nL*.1* 255;
return cV;
}
Item
(Inserted by clicking on the picture icon in the page editor, resized by selecting the dimensions.)
<!-- /#content-output -->
<!-- main ends here --> <!-- sidebars starts here --><!-- sidebars end here -->
<!-- content ends here --> <!-- footer starts here -->
<!-- footer ends here -->