User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Three Fading LEDs and an Origami Crane

Submitted by npdoty on Wed, 09/17/2008 - 23:59

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Three LEDs (one red, one blue, one green) were each put in series with a 220Ω resistor and then connected to a single ground and Arduino pins 9, 10 and 11.  Pins 9, 10 and 11 all support pulse width modulation, which lets us output an intermediate value to the pin and make the LED brighter or dimmer.

Source code (see below) lets the user at the computer control the relative brightness of each LED by sending a string of the letters 'r', 'g' and 'b'.  For example, "rrrrb" will turn the red LED on quite bright and the blue LED just a little bit.

To diffuse the LEDs and try to mix their colors, I experimented with a piece of origami: a paper crane.  A small hole cut in the bottom of the crane lets the LEDs fit inside and the body and wings of the crane take on the mixed color of the lights.  The paper doesn't do a perfect job diffusing though: it's still quite easy to see the individual LEDs.

Code


 

// Fading 3 LED 
// original code by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// heavily modified by npdoty

int value = 0;                            // variable to keep the actual value 
int whichPin = 0;                         // variable to loop through pins array
int whichChar = 0;                        // variable to loop through character array
int numChars = 0;                         // number of characters read from the serial port
int originalValue;                        // starting brightness of a particular LED
int newValue;                             // ending brightness of a particular LED

int pins[3];  //array to hold the pins that I'll write to
char colors[3];
int values[3];

char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;

void setup() 
{ 

    pins[0] = 9;
    colors[0] = 'g';

    pins[1] = 10;
    colors[1] = 'b';

    pins[2] = 11; 
    colors[2] = 'r';

    Serial.begin(9600);
} 

void loop() 
{ 
    numChars = readSerialString(serInString);

    for (whichChar = 0; whichChar < numChars; whichChar ++)
    {
        char color = serInString[whichChar];

        for (whichPin = 0; whichPin < 3; whichPin ++)
        {
            if (color == colors[whichPin])
            {
                originalValue = values[whichPin];
                newValue = originalValue + 50;
                if (newValue > 255)
                {
                    newValue = 255;
                }

                for(value = originalValue + 5 ; value <= newValue ; value+=5) // fade in (from min to max) 
                { 
                    analogWrite(pins[whichPin], value);           // sets the value (range from 0 to 255) 
                    values[whichPin] = value;                  //keep track of the current value
                    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
                }
            } 
        }
    } 

    serInString[0] = 0;  //clear the used string
} 

//read a string from the serial and store it in an array
//you must supply the array variable
int readSerialString (char *strArray) {
    int i = 0;
    if(!Serial.available()) {
        return i;
    }
    while (Serial.available()) {
        strArray[i] = Serial.read();
        i++;
    }

    return i;
}