RGB Light + Diffuser

dheinzerling's picture

Description
 

Use the Arduino to control 3 LEDS using the serial monitor. The user can enter a string of r's, g's, and/or b's to adjust the brightness of each corresponding LED. Each r, g, or b increments the corresponding LED by 10%, until the LED reaches it's maximum brightness, at which point it resets itself back to 0 brightness.

The LEDs are placed together in a diffuser that is made from the white plastic pieces of a regular scotch tape roll. The diffuser has reflective mylar film on the top and bottom to reflect the light within the diffuser and only allow it to escape out of the sides of the diffuser.

Components Used

  • Light Emitting Diodes (LED)
  • Resistors
  • Scotch tape roll
  • Mylar film

Arduino Code

/* 

 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 
 * modified by David Heinzerling 12 September 2011
 */
 
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 red;
int green;
int blue;
 
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,   0);   // set them all to 0
  analogWrite(greenPin, 0);   // set them all to 0
  analogWrite(bluePin,  0);   // set them all to 0
  Serial.println("enter a series of r, g, or b letters to increase red, green, or blue brightness by 10% for each letter :");  
}
 
void loop () {
  int i = 0;
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
  while(i < 100){  
  colorCode = serInString[i];
    if( colorCode == 'r'){
      red = (red + 25) % 250;
      Serial.print("Setting red value to ");
      Serial.print(red);
      Serial.println();
      analogWrite(redPin, red);
      }
    else if(colorCode == 'g'){
      green = (green + 25) % 250;
      Serial.print("Setting green value to ");
      Serial.print(green);
      Serial.println();
      analogWrite(greenPin, green);
      }
    else if(colorCode == 'b'){
      blue = (blue + 25) % 250;
      Serial.print("Setting blue value to ");
      Serial.print(blue);
      Serial.println();
      analogWrite(bluePin, blue);
      }
    i++;
 }
  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++;
  }
}
 
photo1.JPG
0
Your rating: None