[Assignment 2] LED Diffuser

Submitted by chan on Mon, 02/11/2013 - 21:43

 

Description

 
I made LED diffuser with a spoon of crushed ice and wrinkled paper in a small glass bottle. For the optional serial input, I implemented to allow HEX color code such as #ffffff(white), #ffff00(yellow), etc. In order to do so, I needed to use a function that converts HEX to Integer. 
 

Components Used

 
  • Arduino UNO
  • 3* 220 Ohm Resistor, RGB LEDs
  • Crushed Ice, PE Gloves, Small Bottle, Wrinkled Paper
 

Code

 
// Eungchan Kim
// 2013-02-11
 
char inputString[100];
char c;
char color;
int RGB[3] = {0, 0, 0};
int pin[3] = {9, 10, 11};
 
void setup() {
  pinMode(pin[0], OUTPUT); // Red
  pinMode(pin[1], OUTPUT); // Green 
  pinMode(pin[2], OUTPUT); // Blue
 
  Serial.begin(9600);
  analogWrite(pin[0], 125);
  analogWrite(pin[1], 125);
  analogWrite(pin[2], 125);
  Serial.println("Enter HEX color code: #ffffff(white),#ffff00(yellow),#bf00ff(purple)"); 
}
 
void loop () {
  readInputString(inputString);   
  c = inputString[0];
  int i=0, j=0;
  char* temp_char;
 
 //----------default----------------//
  if ( c == 'r' || c == 'g' || c == 'b' ) {
    while (i < 100 && inputString[i] != '\0') {
        color = inputString[i];
        switch (color) {
          case 'r': j=0; break;
          case 'g': j=1; break;
          case 'b': j=2; break;
        }
        RGB[j] = (RGB[j] + 10) % 255;
        analogWrite(pin[j], RGB[j]);
        Serial.print("setting color ");
        Serial.print(color);
        Serial.print(" to ");
        Serial.println(RGB[j]);      
        i++;
    }
  }
 //----------(optional: input by HEX color code)----------------//
  else if (c=='#'){
     // get HEX color code
     temp_char = inputString+1;
     String r=temp_char, g=temp_char, b=temp_char;
     r = r.substring(0,2);
     g = g.substring(2,4);
     b = b.substring(4,6);
     // convert HEX to Integer
     RGB[0]= hex_digit_to_integer(r[0])*16 + hex_digit_to_integer(r[1]);
     RGB[1]= hex_digit_to_integer(g[0])*16 + hex_digit_to_integer(g[1]);
     RGB[2]= hex_digit_to_integer(b[0])*16 + hex_digit_to_integer(b[1]);
     for (int i=0;i<3;i++){
       analogWrite(pin[i],RGB[i]);
     }
     Serial.print("Setting color to ");
     Serial.print(inputString);
     Serial.print(", equal to RGB(");
     Serial.print(RGB[0]);
     Serial.print(",");
     Serial.print(RGB[1]);
     Serial.print(",");
     Serial.print(RGB[2]);
     Serial.println(")");   
  }
  int k;
  for (k=0;k<10;k++) {
    inputString[k] = 0; // initialize all strings
  }
  delay(100);
 
void readInputString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}
// HEX to Integer function
// From Stackflow http://stackoverflow.com/questions/1416571/method-to-convert-from-hex-to-integer-in-c-cant-get-lowercase
int hex_digit_to_integer(char digit) {
    return digit - (digit & 64 ? 55 : 48) & 15;
}
 

Code

 

LED Diffuser with crushed ice

http://youtu.be/AmVNQacmfzs

LED Diffuser with wrinkled paper

http://youtu.be/YXuYesKXJ34

 

 

0
Your rating: None
Drupal theme by Kiwi Themes.