Introduction: Accelerometer Powered LED Dress

Let's be honest for a moment, doesn't everyone want a dress that lights up at your very whim? No? Alright, well I do. It's pretty straightforward, though the programming gets a wee bit tricky. So pick up your pencil, sketch out a design and then we'll talk parts.Here' 

Step 1: Material

Lilypad Simple Snap and Snap Protoboard.  – available on https://www.sparkfun.com/products/10941 for $29.95 and https://www.sparkfun.com/products/10940 - $9.95
Lilypad FTDI board and cable – https://www.sparkfun.com/products/10275 $14.95
LilyPad micro LED’s – shttps://www.sparkfun.com/products/10753 at $3.95 for 5. I used 30 of them. (you can also use the regular sized LED’s but I chose to use the micros.)
LilyPad Accelerometer – https://www.sparkfun.com/products/9267 $24.95
Insulated wire – I used a ribbon cable from Sparkfun, but you can also just purchase it at a local hardware store.
Soldering Iron
Alligator clips and Heat Shrink Tubing
Dress or project on which you want to put your magical lights – I used a skirt I already made several months ago.

Step 2: Schematics

Before jumping right into this project, be sure to draw a basic sketch of what you want your project to look like. Make sure you add in circuits and where everything is going to go.

Step 3: Soldering Lights

Depending on the type of fabric you have, you may be able to use electrically conductive thread (available on SparkFun). I however, did not get that chance as I used an acetate taffeta fabric and actually ended up burning  a hole in my skirt due to a short in my circuit. I therefore, decided to solder  my lights to insulated wire to avoid further burnings.

Cut up a section of the ribbon cable, and split each wire and strip the ends. These are what we will solder to the lights and the accelerometer. I used colored wires as positive and grey as negative.

Make sure you know how to work a soldering iron, or ask someone to help you, as the lights are really small and it’s easy to burn a hole all the way through the light board

Once you’ve soldered the lights, check to make sure the solder worked. Use alligator clips and simply connect them to the positive and negative pins of the LilyPad.

Also, at this point, solder the larger section of the ribbon cable to the Lilypad Snap board. Each pin gets its own wire, make sure to solder a wire to the positive and negative pins as well, we will use them in a bit.

Step 4: Adding Lights to the Dress

Once again, because I used an acetate fabric, I simply had to burn small holes in the fabric for the LED wires to poke through to the underside. I also burned a slightly larger hole to allow the larger ribbon cable and the negative leads to reach the LilyPad board.

After you’ve got all of the LED’s situated onto your project, connect them back up to the larger ribbon cable. Be sure to solder them together and then cover it up with a bit of heat shrink tubing.

By this point you should have all of the lights and the Accelerometer connected to the LilyPad. I hid mine in a drape on my skirt, but you can put your LilyPad board anywhere you like.

Step 5: Programming

Now here’s the tricky part. In order to really get accurate accelerometer readings you need to either wear the skirt or use your project in the way it’s intended to be used. Otherwise you get absolutely useless readings.  I used Arduino as the programming platform. So put on the skirt, hook it up to the computer, and prance around your basement carrying your laptop for a minute or two.

In order to get readings you need to have uploaded code to the Lilypad and then turned on the serial monitor (the magnifying glass in the upper right corner).

Here is the code I used. It averages the accelerometer readings to make it more stable.

//***Code adapted from Programming Interactivity (pp. 234-235) by Joshua Noble***

int led1 = 5;
int led2 = 6;
int led3 = 9;
int led4 = 10;
int led5 = 11;

int xpin = A3;         // x-axis of the accelerometer
int ypin = A4;         // y-axis
int zpin = A5;         // z-axis (only on 3-axis models)

int xVal = 0;
int yVal = 0;
int zVal = 0;

int xVals[8]; //an array of the last 8 x coordinate readings
int yVals[8]; //an array of the last 8 y coordinate readings
int zVals[8]; //an array of the last 8 z coordinate readings
int xAvg = 0; //the x value we'll read eventually
int yAvg = 0;
int zAvg = 0;

int currentSample = 0;



void setup()
{
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    pinMode(led5, OUTPUT);
    pinMode(xpin, INPUT);
    pinMode(ypin, INPUT);
    pinMode(zpin, INPUT);
    Serial.begin(9600);
}

void loop()
{
  ///we use currentSample as an index into the array and increment at the
//end of the main loop)(), so see if we need to reset it at the
//very start of the loop

  if (currentSample == 8) {
    currentSample = 0;
  }
    xVal = analogRead(xpin);
    yVal = analogRead(ypin);
    zVal = analogRead(zpin);
    //This stores 8 values. Basically, it stores
    //the last 8 values read and averages it continually (later in the code).
    xVals[currentSample] = xVal;
    yVals[currentSample] = yVal;
    zVals[currentSample] = zVal;

    //here is where the values are added so that you
    //eventually get the last 8 values

    for (int i=0; i < 8; i++) {
      xAvg += xVals[i];
      yAvg += yVals[i];
      zAvg += zVals[i];
    }
      //These will read the first 7 cycles, but that won't make a huge difference
      //unless you need to read the value from the accelerometer right away

      //Calculate the average
      xAvg = (xAvg / 20);
      yAvg = (yAvg / 20);
      zAvg = (zAvg / 20);

      //Print out the average
      Serial.print(xAvg);
          Serial.print(" ");
      Serial.print(yAvg);
          Serial.print(" ");
      Serial.println(zAvg);
      delay(100);

      currentSample++; //increment the sample

    if (xAvg > 570 && xAvg < 590)
     {
      digitalWrite(led1, HIGH);
      delay(100);
      digitalWrite(led1, LOW);
      delay(100);
      digitalWrite(led2, HIGH);
      delay(100);
      digitalWrite(led2, LOW);
      delay(100);
      digitalWrite(led3, HIGH);
      delay(100);
      digitalWrite(led3, LOW);
     } 

}

Step 6: More Programming

Once you’ve got the readings, the last part is deciding which readings to use. I chose to use the xAvg reading.

Now it’s just the process of adding a simple ‘if’ statement and then program the lights.  Be sure to state the LED’s at the top next to the accelerometer pins. And also to either declare them OUTPUT or INPUT.

Here’s the code I used.

//***Code adapted from Programming Interactivity (pp. 234-235) by Joshua Noble***

int flower1 = 5;       //LED pins
int flower2 = 6;
int flower3 = 9;
int flower4 = 10;
int flower5 = 11;

int xpin = A3;         // x-axis of the accelerometer
int ypin = A4;         // y-axis
int zpin = A5;         // z-axis (only on 3-axis models)

int xVal = 0;
int yVal = 0;
int zVal = 0;

int xVals[8]; //an array of the last 8 x coordinate readings
int yVals[8]; //an array of the last 8 y coordinate readings
int zVals[8]; //an array of the last 8 z coordinate readings
int xAvg = 0; //the x value we'll read eventually
int yAvg = 0;
int zAvg = 0;

int currentSample = 0;



void setup()
{
    pinMode(flower1, OUTPUT);     //set up the pinModes so that the lights will turn off and on.
    pinMode(flower2, OUTPUT);
    pinMode(flower3, OUTPUT);
    pinMode(flower4, OUTPUT);
    pinMode(flower5, OUTPUT);
    pinMode(xpin, INPUT);
    pinMode(ypin, INPUT);
    pinMode(zpin, INPUT);
    Serial.begin(9600);     //initializes communication with your computers
}

void loop()
{
  ///we use currentSample as an index into the array and increment at the
//end of the main loop)(), so see if we need to reset it at the
//very start of the loop

  if (currentSample == 8) {
    currentSample = 0;
  }
    xVal = analogRead(xpin);
    yVal = analogRead(ypin);
    zVal = analogRead(zpin);
    //This stores 8 values. Basically, it stores
    //the last 8 values read and averages it continually (later in the code).
    xVals[currentSample] = xVal;
    yVals[currentSample] = yVal;
    zVals[currentSample] = zVal;

    //here is where the values are added so that you
    //eventually get the last 8 values

    for (int i=0; i < 8; i++) {
      xAvg += xVals[i];
      yAvg += yVals[i];
      zAvg += zVals[i];
    }
      //These will read the first 7 cycles, but that won't make a huge difference
      //unless you need to read the value from the accelerometer right away

      //Calculate the average
      xAvg = (xAvg / 20);
      yAvg = (yAvg / 20);
      zAvg = (zAvg / 20);

      //Print out the average

       Serial.print(xAvg);
          Serial.print(" ");
      Serial.print(yAvg);
          Serial.print(" ");
      Serial.println(zAvg);
      delay(100);

     if (xAvg < 180)       //set the if statement to react depending on your readings.
       {
         digitalWrite(flower1, HIGH);     //turns on the lights and then turns them off again 
         delay(300);
         digitalWrite(flower2, HIGH);
         digitalWrite(flower1, LOW);
         delay(300);
         digitalWrite(flower3, HIGH);
         digitalWrite(flower2, LOW);
         delay(300);
         digitalWrite(flower4, HIGH);
         digitalWrite(flower3, LOW);
         delay(300);
         digitalWrite(flower4, LOW);
       }
     else if(xAvg > 190 && xAvg < 195)
       {
         digitalWrite(flower4, HIGH);
         delay(500);
         digitalWrite(flower4, LOW);
         delay(500);
         digitalWrite(flower3, HIGH);
         delay(500);
         digitalWrite(flower3, LOW);
         delay(500);
         digitalWrite(flower2, HIGH);
         delay(500);
         digitalWrite(flower2, LOW);
         delay(500);
         digitalWrite(flower1, HIGH);
         delay(500);
         digitalWrite(flower1, LOW);
         delay(500);
       }
     else if(xAvg > 195 && xAvg < 215)
        {
          digitalWrite(flower1, HIGH);
          digitalWrite(flower3, HIGH);
          digitalWrite(flower2, LOW);
          digitalWrite(flower4, LOW);
          delay(600);
          digitalWrite(flower1, LOW);
          digitalWrite(flower2, HIGH);
          digitalWrite(flower3, LOW);
          digitalWrite(flower4, HIGH);
          delay(600);
        }


      currentSample++; //increment the sample



}

Step 7: Finishing Up and Problem Solving

Once you’ve got the LilyPad programmed, there’s not much else to do but put it on and prance around. If you’re anything like me, you’ll want to play around with the programming some more, but for all intents and purposes, it works fantastically.

My professor suggested programming the lights to fade according to the accelerometer values, once I figure out how to do that, I will definitely upload that code as well.

Key points:
• Be sure you have all of the materials before starting
• Don’t be discouraged if things don’t work right away, it takes time.
• You may have to re-solder some LED’s, this is normal.
• Be sure to tack down the wire on the underside of the skirt so that they don’t get pulled as you put the dress on and off.
• The programming is the tricky part, don’t be afraid to ask someone who knows more to help you.