Lab 2 - Serial LED + Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:

Description

I altered the Serial RGB LED code to allow for strings of color codes ("rrbbbbg") to be inputted by the Serial Window. Each letter sets the corresponding LED to 10 percent brightness. Any unincluded colors remain unchanged.

For the diffuser, I tried the cotton fluff from class as well as packing foam from home.

Components Used

  • 3Light Emitting Diode (LED)
  • 3 Resistors
  • Packing Foam

Arduino Code

/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorCode>...<colorCode>", where "colorCode" is
* one of "r","g",or "b" in any order/combination up to ten letters each. Each letter
* E.g. "rr" turns the red LED to 20 percent brightness.
* "rggbg" turns the red LED to 10 percent brightness, green LED to 30 percent brightness, blue LED to 10 percent brightness.
*
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Adapted 16 September 2009
* Kurt Sookwongse
*/

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 pressesR;
int pressesG;
int pressesB;

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, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'rrrrgg') :");
}

void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);

colorCode = serInString[0];

//count number of times each color was pressed
pressesR = countPresses(serInString, 'r');
pressesG = countPresses(serInString, 'g');
pressesB = countPresses(serInString, 'b');

if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
int i = 0;
int presses = 0;
while (i<3){ // cycle through each color

if (i == 0){
colorCode = 'r';
presses = pressesR;
} else if (i == 1) {
colorCode = 'g';
presses = pressesG;
} else if (i == 2) {
colorCode = 'b';
presses = pressesB;
}

colorVal = presses * 255/10; // convert percentage to LED value
if (presses != 0) { // change color only if color is pressed
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(presses*10); // number of presses * 10 = percent brightness
Serial.print("% brightness");
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);
}

i++;
}
Serial.println("enter color command (e.g. 'rrrrgg') :"); 0
}

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++;
}

strArray[i]='/';
}

//read through array, count number of appearances of color code
//return number of appearances
//you must supply the array variable and character variable
int countPresses (char *strArray, char code) {
int count = 0;
int i = 0;
while (strArray[i]!='/' & count <10){
if( strArray[i] == code){
count++;}
i++;
}
return count;
}

Item

attached images