Lab 2 - 3 color LED diffuser with transition

Submitted by seanchen on Tue, 02/12/2013 - 23:40

Description

First, a bottle cap wrapped with Styrofoam was used as the diffuser. Then I found out that the LED was still pretty distinct so I had to add another layer inside the cap. I extended the sample code so that it catches a char which is mapped to a color. 

In version 2, I added the transition effect. For each change, the change in rgb values are divided by multiple cycles and run in a loop so that the light gradually transit to the next color. Right now it's set to have 50 cycles that transit in 1 second. In addition, a help instruction is shown for easier control. 

The LEDs are not perfectly precise. Quite some time was spent to fine-tune the values. However, yellow and brown are still not as accurate.

http://youtu.be/TJ7f3dpzwXs

 

Components

1 - Arduino UNO Board

3 - 220-ohm resistors
3 - LED lights (1 red, 1 blue, 1 green)
1 bottle cap
Some Styrofoam as diffuser

 

Code

char serInString[10];  // 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 curR = 127;  //the initial value
int curG = 127;  //the initial value
int curB = 127;  //the initial value
 
int cycle = 50;
boolean fade = true;
boolean debug = false;
char help[] = "Type a letter to switch the color.\nRed:r, Green:g, Blue:b\nWhite:i, Dimmed:d, Black:k\nPurple:p, Yellow:y, Cyan:c\nPink:n, Orange:o, Brown:w\n";
 
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(help);  
}
 
void loop () {
  // clear the string
  memset(serInString, 0, 10);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
  colorCode = serInString[0];
 
  if(colorCode != '') {
    switch(colorCode) {
    case 'r':
      changeLED(255,0,0);
      break;
    case 'g':
      changeLED(0,255,0);
      break;
    case 'b':
      changeLED(0,0,255);
      break;
    case 'i':
      changeLED(255,255,235);
      break;
    case 'd':
      changeLED(127,127,127);
      break;
    case 'k':
      changeLED(0,0,0);
      break;
    case 'o':
      changeLED(255,95,0);
      break;  
    case 'p':
      changeLED(255,0,255);
      break;  
    case 'y':
      changeLED(255,130,0);
      break;       
    case 'n':
      changeLED(255,65,78);
      break;
    case 'c':
      changeLED(0,135,115);
      break;
    case 'w':
      changeLED(255,70,0);
      break;      
    case '?':
      Serial.println(help);
      break;
    default:
      Serial.println("This is not in the options");
      Serial.println(help);
    }   
  } 
  
  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++;
  }
}
 
void changeLED(int r, int g, int b) {
  Serial.print("Changing to: "); 
  Serial.print(r); 
  Serial.print(','); 
  Serial.print(g);
  Serial.print(','); 
  Serial.println(b); 
 
  if(fade) {
    fadeLED(r,g,b); 
  } else {
  analogWrite(redPin,   r);   
  analogWrite(greenPin, g);   
  analogWrite(bluePin,  b);  
    
  }
}
 
void fadeLED(int r, int g, int b) {
 
  
  int deltaR = (r-curR)/cycle;
  int deltaG = (g-curG)/cycle;
  int deltaB = (b-curB)/cycle;
  if(debug) {
    Serial.println(deltaR); 
    Serial.println(deltaG);
    Serial.println(deltaB);    
  }
    
  for(int i=cycle; i>0; i--) {
 
    analogWrite(redPin,   curR+=deltaR);   
    analogWrite(greenPin, curG+=deltaG);   
    analogWrite(bluePin,  curB+=deltaB);
    if(debug) {    
      Serial.println(i);
      Serial.println(curR); 
      Serial.println(curG);
      Serial.println(curB);
    }
    delay(20);
  }
  
  curR = r;
  curG = g;
  curB = b;
  
  analogWrite(redPin,   curR);   
  analogWrite(greenPin, curG);   
  analogWrite(bluePin,  curB);  
  
  if(debug) {
    Serial.println(curR); 
    Serial.println(curG);
    Serial.println(curB);
  }
}
 
blue
red
top
0
Your rating: None
Drupal theme by Kiwi Themes.