Introduction: Nursery Rhyme Hat

I created the Nursery Rhyme Hat for a class on youth and technology.  I'm new to instructables and e-textiles, so please bear with me!
This is the first time I have used the Lilypad Arduino or any kind of soft circuit technology, so my coding is borrowed from several others (more on my code in Step 2). 
All in all, it was quite the learning experience creating this hat and I'm excited to share it with you!

Step 1: Gather Materials

Materials Needed:
-Lilypad Arduino Development Board
https://www.sparkfun.com/products/11262
            -Lilypad Simple Board, 1 White LED, RGB tricolor LED, Light Sensor, Buzzer, FTDI, Conductive Thread, Needles, LiPo Battery
-USB cable
-Fedora
-Silk flower rosette
-Small rectangle of green felt (~2x.5in)
-Large rectangle of lining fabric (~5x4in)
-2 Sets of hook-and-eyes, plus one extra hook
-All purpose thread
-Clear nail polish
-Needlenose pliers


Step 2: Create a Plan

I started with an idea for a hat that you could wear during a storytime with young children.  When exposed to light (lifting the flower's felt leaf) the hat would light up and play a nursery rhyme tune as a fun prop for storytime.  I used or learned from code from Leah Buechley's Sound example, Casey's Light Sensor code for her Night and Day Toy, Alyson's Dark-Be-Gone code and Leah Buechley's Tricolor LED code.  I ended up with a code that made my hat light up and play Mary Had a Little Lamb (See Below).

Plug your FTDI and battery into the main board and coonect it to your computer with your USB cable.  Upload your code.


/*
Nursery Rhyme Hat
Liz Huessy
March 2014
*/
int ledPin =
int ledPin = A3;  // The white LED pin is connected to pin A3
int redPin = 10;  // The red LED pin is connected to pin 10
int bluePin = 9;  // The blue LED pin is connected to pin 9
int greenPin = 6;// The green LED pin is connected to pin 6
int ground = 11; // Pin 11 is the ground pin
int speakerPin = A2;     // The speaker is connected to digital pin A2
int sensorPin = A4;     // The light sensor is connected to pin A4
int sensorValue;     // Variable to store the value coming from the sensor

void setup()
{

  pinMode(speakerPin, OUTPUT);     // Sets this pin to be an output
  pinMode(A3, OUTPUT);  // Sets this pin to be an output
  pinMode(10, OUTPUT);  // Sets this pin to be an output
  pinMode(9, OUTPUT);  // Sets this pin to be an output
  pinMode(6, OUTPUT);   // Sets this pin to be an output
  pinMode(sensorPin, INPUT); //
  pinMode(ground, OUTPUT);
  digitalWrite(ground, LOW);  // Turns off power to pin 11 (ground)

}

void loop()     // Runs on a loop
{

  sensorValue = analogRead(sensorPin);     // Read the value from the sensor
  delay(100);     // Delay for 1/10 of a second

  if (sensorValue < 20)// If it is dark, turn all LEDs off
  {
    digitalWrite(6, LOW);
    delay(750);       
    digitalWrite(9, LOW);
    delay(500);
    digitalWrite(10, LOW);
    delay(500);
    digitalWrite(A3, LOW);
    delay(500);
  }
  else {  //If it is not dark, turn green and blue LEDs on and play the tune, then turn on red and white LEDs
    digitalWrite(6, HIGH);
    delay(500);
    digitalWrite(9, HIGH);
    delay(500);
    scale();     // Call the scale() function
    delay(1000);     // Delay for 1 second
    digitalWrite(10, HIGH);
    delay(500);
    digitalWrite(A3, HIGH);
    delay(500);

  }
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)     // the sound producing function
{
  int x;
  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  for (x=0;x<loopTime;x++)
  {
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayAmount);
  }
}

void scale ()//Play Mary Had a Little Lamb
{
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,4186,500);  //C
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2637,500); //E
  beep(speakerPin,2637,1000); //E

  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2637,1000);  //E

  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,4186,500);  //C
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2637,500); //E
  beep(speakerPin,2637,1000); //E

  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,2637,500);  //E
  beep(speakerPin,2349,500);  //D
  beep(speakerPin,4186,1000);  //C
  beep(speakerPin,4186,1000);  //C
}


Once I had written my code, the next thing I did was to create a diagram of my e-textile, showing the exact connections and layout of my circuit, making sure that no negative and positive lines crosssed and that each LED, Buzzer and Sensor was properly connected.
It is very important to complete this step before taking apart the Lilypad pieces!

Step 3: Sew Electronics

Now you are ready to carefully break apart the Lilypad pieces.  I used a pair of needle nose pliers, but I'm sure tweezers could also work. 
With the conductive thread, you now begin to sew on the LEDs, Buzzer, Light Sensor and Main Lilypad Board, using the diagram from the previous step.
Each sewn connection should begin and end with sewing the conductive thread through the pin multiple times to ensure a good connection.

Step 4: Add the Rosette

Next, attach the rosette to the hat with nonconductive thread, by stitching along the very top of the flower.  Do not sew all the way around the flower, or you will not be able to use the hook-and-eye closure effectively.
I also threw in a few stitches to anchor the fabric around the LED, ensuring that the light would shine through.

Step 5: Attach the First Hook-and-Eye Set

You will use a total of two sets of hook-and-eyes and one extra "eye" for this project. 
Start with attaching the first hook to the hat below the lines you have created with conductive thread.  Then, attach an eye to the rosette.  Now you have a closure that will allow you to turn the power on and off, charge the battery or even upload new code without disturbing the rest of the design.

Step 6: Attach the Second Hook-and-Eye Set

The next step is to sew your green felt leaf onto the hat so that it covers the light sensor.
First, use nonconductive thread to sew the side of the leaf that will be hidden by the flower to the hat. Be sure to position the leaf to cover the light sensor well.
Next, sew a hook onto the other end of the leaf and a corresponding eye onto the hat, so that the light sensor is covered when not in use.
Last, sew the extra eye under the petals of the rosette.  This extra fastener allows you to hook the leaf up uncovering the light sensor.

Step 7: Seal the Knots and Attach the Lining

Next, you need to make sure that all your lose ends and knots of thread on the inside of the hat are sealed, so they do not touch each other and short the circuit. 
I used clear nail polish to seal all the knots. 
To make the hat safe and comfortable to wear, you need to line the inside of the hat to cover the conductive threads.
I did not line the whole hat, but only the area in which the conductive thread showed through.

Step 8: Watch the Hat in Action!

You've finished the hat.
Turn it on and watch it play!

http://youtu.be/VDFglFQgGuw


If that does not work, try
https://www.youtube.com/watch?v=VDFglFQgGuw&feature=youtu.be