Diffuser and Changing Lights

IMG_1514Description:

In this assignment, we expanded our 1 blinking LED light circuit to be 3 LED blinking lights, and then explored how different diffusers change how we perceive the light, and how we can modify the code to take user input.

I looked through past student’s presentations for examples (http://courses.ischool.berkeley.edu/i290-13/f07/assignments/39.html) and I decided to play around with users increasing/decreasing the LED light brightness. You increase the light with a capital letter (R = red, B = blue, G = green) and decrease with a lowercase letter.

For the diffuser I tried a few different techniques. I initially used the cotton provided in class, and decided that I wanted to see how colored diffusers made a difference. I found some black tissue paper and wrapped it around, and put all of it in a uniqlo bag. It worked really well if the brightness was at max.

Components:

  • 1x Arduino Uno
  • 1x Breadboard
  • Breadboard wires
  • 3x LEDs
    • 1 Red
    • 1 Green
    • 1 Blue
  • 3x 220Ω Resistors
  • USB cable

Code:

/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* Clay Shirky <clay.shirky@nyu.edu>
*
* Adapted by Ken-ichi Ueda <kueda@ischool.berkeley.edu>
* Adapted by Michelle Carney from http://courses.ischool.berkeley.edu/i290-13/f07/assignments/39.html
*/

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

// Program variables
int redVal= 127;
int greenVal = 10;
int blueVal = 10;
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;

int wait = 5; // millesconds to wait btwn fade increments
int inc = 10; // increment value for brightening or dimming LEDs
int i = 0; // Loop counter
char cmd; // holds a command character
char tled; // target LED of a command ('r', 'g', 'b')

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
writeAllValues();

Serial.println("Commands:");
Serial.println(" r -- decreases red LED brightness by 10");
Serial.println(" RRR -- increases red LED brightness by 30");
Serial.println(" fr -- fade out red LED");
Serial.println(" Fr -- fade in red LED to max brightness");
Serial.println();
Serial.println("Enter a command: ");
}

// Main program
void loop()
{
readSerialString(serInString, 100);
cmd = serInString[0];

// fade controls
if (cmd == 'f') {
//Serial.println("Fade out requested...");//test
tled = serInString[1];
fadeOut(tled);
}
else if (cmd == 'F') {
//Serial.println("Fade in requested...");//test
tled = serInString[1];
fadeIn(tled);
}
// brightness control: look for rgb chars, uppercase brightens and lowercase
// dims
else if (cmd == 'r' || cmd == 'R' || cmd == 'g' || cmd == 'G' || cmd == 'b' || cmd == 'B') {
Serial.println("Changing brightness...");//test
for(i=0; i < 100 && serInString[i] != '\0'; i++) {
//Serial.println("Looping over brightness commands...");//test
switch (serInString[i]) {
case 'r':
redVal -= inc;
break;
case 'R':
redVal += inc;
break;
case 'g':
greenVal -= inc;
break;
case 'G':
greenVal += inc;
break;
case 'b':
blueVal -= inc;
break;
case 'B':
blueVal += inc;
break;
}
}

trimValues();
writeAllValues();
}

resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}

// ensure brightness remains within 0-255 range
void trimValues() {
if (redVal > 255) {
redVal = 255;
}
else if (redVal < 0) {
redVal = 0;
}
if (greenVal > 255) {
greenVal = 255;
}
else if (greenVal < 0) {
greenVal = 0;
}
if (blueVal > 255) {
blueVal = 255;
}
else if (blueVal < 0) {
blueVal = 0;
}
}

// write all brightness values to the pins
void writeAllValues() {
//Serial.println("Writing values...");//test
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}

// fade in LED in by pin number
void fadeIn(int pin) {
Serial.print("Fading in pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal < 255) {
redVal += 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal < 255) {
greenVal += 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal < 255) {
blueVal += 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade in LED in by color
void fadeIn(char colorCode) {
Serial.print("Fading in ");
Serial.print(colorCode);
Serial.println(" pin...");
switch (colorCode) {
case 'r':
fadeIn(redPin);
break;
case 'g':
fadeIn(greenPin);
break;
case 'b':
fadeIn(bluePin);
break;
}
}

// fade out LED in by pin number
void fadeOut(int pin) {
Serial.print("Fading out pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal > 0) {
redVal -= 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal > 0) {
greenVal -= 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal > 0) {
blueVal -= 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade out LED in by color
void fadeOut(char colorCode) {
switch (colorCode) {
case 'r':
fadeOut(9);
break;
case 'g':
fadeOut(10);
break;
case 'b':
fadeOut(11);
break;
}
}

// full input arr with null values
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++;
}
}

Leave a Reply