soundie: a musical touch-sensitive light-up hoodie

Step 10Program your jacket

program your jacket
Figure out what you want your garment to do.
There's plenty of flexibility with this speaker and LED and conductive fabric design. The program reads the analog input (that is, your conductivity) as a number between 0 and 1023; you can change the behavior of the jacket depending on the range that the number is in. For instance, when you are more conductive, the analog input is lower. More information on using analog sensors is available here.

I programmed my hoodie to fade the sets of LEDs from one to another when the conductivity is low. Then, with each range of conductivity values, I had the speaker emit a higher noise, and had a different set of LEDs light up.

Here's the code I used to get this behavior:

int sensorPin = 0; // fabric sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
int pin5 = 5; // LED connected to digital pin 5
int pin6 = 6; // LED connected to digital pin 5
int pin7 = 7; // LED connected to digital pin 5
int pin8 = 8; // LED connected to digital pin 5
int speakerPin = 9; // speaker connected to digital pin 9

void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize the serial port
digitalWrite(14, HIGH); // sets analog pin a0 to high
pinMode(pin5, OUTPUT); // sets digital pin 5 to be an output
pinMode(pin6, OUTPUT); // sets digital pin 6 to be an output
pinMode(pin7, OUTPUT); // sets digital pin 7 to be an output
pinMode(pin8, OUTPUT); // sets digital pin 8 to be an output
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
}

void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // send that value to the computer
if (sensorValue < 1000 && sensorValue >= 970)
{
digitalWrite(pin5, HIGH); //turns on led connected to pin 5
digitalWrite(pin6, HIGH); //turns on led connected to pin 6
digitalWrite(pin7, HIGH); //turns on led connected to pin 7
digitalWrite(pin8, HIGH); //turns on led connected to pin 8
}
else if (sensorValue <= 970 && sensorValue > 890)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
beep(speakerPin,2093,100); //C: play the note C 500ms
}
else if (sensorValue <= 890 && sensorValue > 810)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
beep(speakerPin,2349,100); //D
}
else if (sensorValue <= 810 && sensorValue > 730)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, LOW);
beep(speakerPin,2637,100); //E
}
else if (sensorValue <= 730 && sensorValue > 660)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
beep(speakerPin,2793,100); //F
}
else if (sensorValue <= 660 && sensorValue > 590)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
beep(speakerPin,3136,100); //G
}
else if (sensorValue <= 590 && sensorValue > 520)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, LOW);
beep(speakerPin,3520,100); //A
}
else if (sensorValue <= 520 && sensorValue > 460)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
beep(speakerPin,3951,100); //B
}
else if (sensorValue <= 460)
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
beep(speakerPin,4186,100); // high C
}
else
{
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
}
}

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);
}
}

Other possibilities (changing only the section under "void loop()"):

You can change the section under the "void loop()" (be sure to preserve the open bracket at the beginning and close bracket at the end) to something like:

sensorValue = analogRead(sensorPin); // read the value from the sensor
if (sensorValue < 1000)
beep(speakerPin, sensorValue*3, 100);

This will allow a more direct response between how you touch the conductive fabric and what your speaker outputs. The more of the fabric you're touching, the lower the note will be. This is the behavior shown in the video.

If you want the note to become higher when you touch more of the fabric, you can do something like:

sensorValue = analogRead(sensorPin); // read the value from the sensor
if (sensorValue < 1000)
beep(speakerPin, 3072 - sensorValue*3, 100);

Of course, you can still add in code to affect the LEDs - this only changes the behavior of the speaker.

Program your jacket.
To program your jacket, paste the code above (or write your own code) in the Arduino window and load it onto the LilyPad. If you aren't sure how to program the LilyPad or if the code makes no sense, Leah Buechley has some great instructions here.

Plug in the battery, and have your garment make some music!
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
14
Followers
1
Author:kanjun