Diffuser and fading LED control

Nicholas's picture

Description

For my first diffuser, I tried using multiple disposable tea bags (the type for loose leaf tea). However, the individual LEDs could still be seen clearly through the bags. For my final diffuser, I placed a disposable tea bag inside an origami inflatable ball. This seemed to work much better.

I created a keyboard-controllable color fader. It utilizes structs to store color values. On initialization, the color fades from green to blue and back. Typing "fast" increases the fading speed, and typing "slow" decreases the speed. The user can also type "red", "green", "blue" or "orange" to change one of the faded colors at any time.

Components

  • 3 LEDs (red, blue, green)
  • 3 220k resistors
  • Arduino
  • Square of printer paper
  • Disposable tea bag

Code

 

/* 
 * INFO262 - S5: Digital I/O with Arduino Boards
 * 
 * Author: Nicholas Kong
 * Date: September 13, 2011
 */
 
#define LOOP_POLLING_FREQUENCY 25;
 
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
 
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  
 
int loopsSincePolled = LOOP_POLLING_FREQUENCY;
 
typedef struct {
  int r;
  int g;
  int b;
} RGBval;
 
RGBval startRGB = {0,0,255};
RGBval endRGB = {0,255,0};
RGBval curRGB = {0,0,255};
RGBval tempRGB;
 
struct {
  boolean r;
  boolean g;
  boolean b;
} incStruct;
 
int loopDelay = 50;
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  
  Serial.begin(9600); // Start serial input
  
  initIncStruct();
  
  // Set pins to the start RGB value
  analogWrite(redPin,   curRGB.r);
  analogWrite(greenPin, curRGB.g);
  analogWrite(bluePin,  curRGB.b);
}
 
void loop () {
  
  // Check if we need to read the serial port for commands
  loopsSincePolled--;
  if(loopsSincePolled < 0) {
    loopsSincePolled = LOOP_POLLING_FREQUENCY;
    readSerialString(serInString);
    
    String str = String(serInString);
    if(str.equalsIgnoreCase("fast")) {
      Serial.println("Fading quickly...");
      loopDelay = 15;
    }
    else if(str.equalsIgnoreCase("slow")) {
      Serial.println("Fading slowly...");
      loopDelay = 50;
    }
    else if(str.equalsIgnoreCase("red")) {
      Serial.println("Fading to red...");
      endRGB.r = 255;
      endRGB.g = 0;
      endRGB.b = 0;
      initIncStruct();
    }
    else if(str.equalsIgnoreCase("green")) {
      Serial.println("Fading to green...");
      endRGB.r = 0;
      endRGB.g = 255;
      endRGB.b = 0;
      initIncStruct();
    }    
    else if(str.equalsIgnoreCase("blue")) {
      Serial.println("Fading to blue...");
      endRGB.r = 0;
      endRGB.g = 0;
      endRGB.b = 255;
      initIncStruct();
    }
    else if(str.equalsIgnoreCase("orange")) {
      Serial.println("Fading to orange...");
      endRGB.r = 242;
      endRGB.g = 133;
      endRGB.b = 55;
      initIncStruct();
    }    
  }
  
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  //readSerialString(serInString);
  
  if(curRGB.r == endRGB.r && curRGB.g == endRGB.g && curRGB.b == endRGB.b) {
    tempRGB = endRGB;
    endRGB = startRGB;
    startRGB = tempRGB;
    initIncStruct();
  }
  
  increment();
  analogWrite(redPin,   curRGB.r);
  analogWrite(greenPin, curRGB.g);
  analogWrite(bluePin,  curRGB.b);
  
  serInString[0] = 0; // Clear string
  
  delay(loopDelay);
}
 
void readString(String* str) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    str->concat(Serial.read());
  }
}
 
//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 initIncStruct() {
  incStruct.r = startRGB.r < endRGB.r;
  incStruct.g = startRGB.g < endRGB.g;
  incStruct.b = startRGB.b < endRGB.b;
}
 
void increment() {
  curRGB.r = incStruct.r ? min(curRGB.r + 5, endRGB.r) : max(curRGB.r - 5, endRGB.r);
  curRGB.g = incStruct.g ? min(curRGB.g + 5, endRGB.g) : max(curRGB.g - 5, endRGB.g);
  curRGB.b = incStruct.b ? min(curRGB.b + 5, endRGB.b) : max(curRGB.b - 5, endRGB.b);
}
 

 

 

Fader with diffuser
0
Your rating: None