Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Description

I created a diffuser using a piece of paper and making an origami ball in order to fit over the LEDs. The user can adjust the lights with multiple key presses of 'r', 'g', or/and 'b'. For example, "rrr" will increase the red LED by 30%.

Components Used

  • Red Light Emitting Diode (LED)
  • Green Light Emitting Diode (LED)
  • Blue Light Emitting Diode (LED)
  • 3 Resistors
  • Paper

Arduino Code

/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>*", where "colorCode" is
* one of "r","g",or "b" and * states thate the command can be repeated many times. Each colorCode increaeses
* the light by 10%.
*
* E.g. "rrr" Increases the red LED by 30%.
* "rrggbb" Increases the red, green and blue LED light by 30%.
* "rgggg" Increases the red LED by 10% and the green LED by 40%.
*
* Note that the light accumlates until it reaches 255, then the light resets.
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Modifed September 13, 2009
* By Lita Cho
*/

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

int redPin = 10; // Red LED, connected to digital pin 9
int greenPin = 9; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

int redValue = 127;
int greenValue = 127;
int blueValue = 127;

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("Enter a color command (e.g. 'rrrgg') :");
}

void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
parseInput(serInString, 100);
readSerialString(serInString);
delay(100); // wait a bit, for serial data
}

// Method to parse the inputs
void parseInput(char *strArray, int length) {
int i = 0; // Counter
int colorCode;
while ( i < length && strArray[i] != '\0') {
colorCode = strArray[i];

if(colorCode == 'r') {
redValue = (redValue + 10) % 255;
Serial.print("Red Value: ");
Serial.println(redValue);
analogWrite(redPin, redValue);
} else if(colorCode == 'g') {
greenValue = (greenValue + 10) % 255;
Serial.print("Green Value: ");
Serial.println(greenValue);
analogWrite(greenPin, greenValue);
} else if(colorCode == 'b') {
blueValue = (blueValue + 10) % 255;
Serial.print("Blue Value: ");
Serial.println(blueValue);
analogWrite(bluePin, blueValue);
}

++i;
}
}

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



Picture

Lab 2