Fuzzy Foam Mood Light

Submitted by mschiff on Tue, 02/12/2013 - 23:02
mschiff's picture

Description:

For this assignment I used the PWM capabilities of the Arduino board to vary the brightness of three LEDs (red, green, and blue) to create a color mixing effect. The brightnesses can be controlled in 2 ways: 1. Manually, via a potentiometer 2. Serially, by sending color names (i.e "red", "purple", "cyan", etc.) or integer values for each light "(x,y,z)" where x, y, and z are interpretted as the value of the red, green, and blue LEDs respectively. When the sketch starts, it is in manual mode, and the user can sweep through a mix of colors by turning the knob. Should they want to specify a color serially, they first send the "knob_off" command. This tells the Arduino not to set the LED values based on the knob. The light will now stay a constant color (regardless of knob position) until a valid serial command is sent. The user can now send messages like "cyan", "purple", or "(170,200,54)" to set the color. Should the user desire to go back to manual control, they send the "knob_on" message, and the Arduino will now recalculate each LEDs value based on the knob position every iteration of the main loop.

Once the code and circuit were working I scoured my apartment for diffuser material. I used a piece of closed-cell styrofoam, which already had a cylindrical hole large enough to fit the three LEDs. Since the diffuser was too thick, I peeled off cells until I had as close to a 1 cell thickness as I could achieve. This allows lots of light to get through, but still provides a diffusing effect. Since the diffuser is made up of lots of individual cells, this technique also yields an interesting texture, as diffusion is not constant across an individual cell. I also opened up the top of the diffuser to let more light out, producing a sort of lamp effect.

Components:

  • 1 - Arduino Board
  • 3 - LEDs (1 red, 1 green, 1 blue)
  • 3 - Resistors (value depends on LEDs)
  • 1 - Breadboard
  • 1 - USB power supply
  • 1 - Closed Cell Styrofoam chunk

Code:

    const int red = 9;
    const int green = 3;
    const int blue = 10;
    const int knob = 0;

    //initial values for the led
    int redVal = 255;
    int greenVal = 1;
    int blueVal = 1;
    //initialize a variable to hold the knobs value
    int knobVal;

    //a charachter buffer for Serial messages
    char buffer[20];

    //a variable used by the loop to decide whether or not
    //to set the LED values based on the knob
    int flag = 1;

    void setup() {
        Serial.begin(9600);
    }

    void loop() {
        //if there is serial data available
        if (Serial.available() > 1) {
            int index = 0; //for iterating through the buffer
            while (Serial.available() > 0) { //while there is data
                buffer[index] = Serial.read(); //read a byte of data
                index++; //increment buffer location
                if (index > 18) { break; } //break if we are going to overflow the buffer
                delay(2); //If i comment this out, messages are broken up -> wait a little in case serial data was delayed
            }
            buffer[index] = '\0'; //null terminate the buffer
            //Send back the command they typed
            Serial.print("You typed: ");
            Serial.println(buffer);
    
            //Conditional block to decide what action to take
            if (!strcmp(buffer, "knob_off")) { flag = 0; } // now we dont do the knob block
            else if (!strcmp(buffer, "knob_on")) { flag = 1; } // now we DO the knob block
            //set LED values for the color entered
            else if (!strcmp(buffer, "purple")) {
                redVal = 148;
                greenVal = 0;
                blueVal = 211;
            } else if (!strcmp(buffer, "orange")) {
                redVal = 255;
                greenVal = 140;
                blueVal = 0;
            } else if (!strcmp(buffer, "cyan")) {
                redVal = 0;
                greenVal = 255;
                blueVal = 255; 
            } else if (!strcmp(buffer, "red")) {
                redVal = 255;
                greenVal = 0;
                blueVal = 0;
            } else if (!strcmp(buffer, "green")) {
                redVal = 0;
                greenVal = 255;
                blueVal = 0;
            } else if (!strcmp(buffer, "blue")) {
                redVal = 0;
                greenVal = 0;
                blueVal = 255;
            } else if (buffer[0]=='(') {
                redVal = atoi(&(buffer[1]));
                greenVal = atoi(&(buffer[3]));
                blueVal = atoi(&(buffer[5]));
                Serial.print("redVal: ");
                Serial.println(redVal);
                Serial.print("greenVal: ");
                Serial.println(greenVal);
                Serial.print("blueVal: ");
                Serial.println(blueVal);
            }
            //This last block checks to the see if the message starts with '(' if so,
            //it is interpretted as (r,g,b)
        }
    
        //if the flag is on we set LED values based on knob position
        if (flag) {
            //Set brightnesses based on knob position
            knobVal = map(analogRead(knob), 0, 1024, 0, 255);
            redVal = knobVal;
            blueVal = 255-knobVal;
            if (knobVal <= 127) { greenVal = 2*knobVal; }
            else { greenVal = (510-(2*knobVal)); }
        }
  
        //write to leds
        analogWrite(green, greenVal);
        analogWrite(red, redVal);
        analogWrite(blue, blueVal);  
    }

 

0
Your rating: None
Drupal theme by Kiwi Themes.