HW2 - LED Diffuser

Submitted by shaohan on Mon, 02/11/2013 - 23:21

Description:

I used one foam peanut and one plastic can to create the diffuser. I first tried to use the plastic can only to make the diffuser, but realized that the mixed color wasn't well distributed. As a result, I then stabbed all LED lights into a foam peanut before covering them with the plastic can, and found that the color-mixing effect worked better in this way.

For multiple key press section, I set the default brightness to 10 for all red, green and blue lights. When user inputs r, g or b, the brightness of that color increases 45. When the brightness excess 225, the value will be reset to 0. For example, the result of 'rrgggbbbb' will be: brightness of red = 90, brightness of green = 135, brightness of blue = 180.

Components Used: 

1 Arduino Uno

3 220 Ohm Resistor 

3 LEDs (R, G, B) 

1 Plastic Bottle 

1 Foam Peanut 

 

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
 *
 * Alternate command structure is "<colorCode>*", where "colorCode" is
 * one of "r","g", or "b".
 * E.g. "r"    increases the red LED brightness by 10
 *      "rrr"  increases the red LED brightness by 30
 *      "ggb"  increases the green LED brightness by 20 and the blue by 10
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 *
 * Adapted 11 Feburary 2013
 * copylefter 2013 Shaohan Chen <shaohan@ischool.berkeley.edu>
 *
 */
 
//include support for manipulating strings.
//for a useful string comparison function, see the bottom of this file... stringsEqual()
#include <stdio.h>
#include <string.h>
 
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 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
 
int redValue = 30;   // Set default value of Red LED brightness to 30
int greenValue = 30; // Set default value of Green LED brightness to 30
int blueValue = 30;  // Set default value of Blue LED brightbess to 30
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   redValue);   // set Red LED to brightness 30
  analogWrite(greenPin, greenValue);   // set Green LED to brightness 30
  analogWrite(bluePin,  blueValue);   // set Blue LED to brightness 30
  Serial.println("enter color command (e.g. 'rrrbbb') :");  
}
 
void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString, 100);
  
  //Uncomment the following line to read commands of the form 'rrrb'
  processRepeatKeyCommands(serInString, 100);
  
 
  //Erase anything left in the serial string, preparing it for the 
  //next loop
  resetSerialString(serInString, 100);  
 
  delay(100);  // wait a bit, for serial data
}
 
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++;
  }
}
 
//go through the string, and increase the red value for each 'r',
//the green value for each 'g', and the blue value for each 'b'.
//For example "rrrg" increases red by 30 and green by 10.
void processRepeatKeyCommands(char *strArray, int maxLength) {
  int i = 0;
  int redCount = 0;
  int greenCount = 0;
  int blueCount = 0;
  
  //loop through the string (strArray)
  //i = the current position in the string
  //Stop when either (a) i reaches the end of the string or
  //                 (b) there is an empty character '\0' in the string
  while (i < maxLength && strArray[i] != '\0') {
    //Read in the character at position i in the string
    colorCode = serInString[i];
    
    //If the character is r (red)...
    if (colorCode == 'r') {
      //Add the redCount counter. If the counter pass 5, then set the brightness back to 0.
      redCount++;
      if (redCount <= 5) {
       redValue = redCount * 45; 
       analogWrite(redPin, redValue);
       Serial.print("setting color red to ");
       Serial.println(redValue);
      } else {
       redValue = 0;
      }
 
      
    //If the character is g (green)...
    } else if (colorCode == 'g') {
      //Add the greenCount counter. If the counter pass 5, then set the brightness back to 0.
      greenCount++;
      if (greenCount <= 5) {
        greenValue = greenCount * 45;
        analogWrite(greenPin, greenValue);
        Serial.print("setting color green to ");
        Serial.println(greenValue);
      } else {
        greenValue = 0;
      }
      
    //If the character is b (blue)...
    } else if (colorCode == 'b') {
      //Add the blueCount counter. If the counter pass 5, then set the brightness back to 0.
      blueCount++;
      if (blueCount <= 5) {
        blueValue = blueCount * 45;
        analogWrite(bluePin, blueValue);
        Serial.print("setting color blue to ");
        Serial.println(blueValue);
      } else {
        blueValue = 0;
      }
    }
    
    //Move on to the next character in the string
    //From here, the code continues executing from the "while" line above...
    i++;
  } 
}

 

0
Your rating: None
Drupal theme by Kiwi Themes.