Archive | December, 2011

Adding sound and using conductive thread as a pressure sensor

6 Dec

In our last meeting we used the conductive thread as a pressure sensor that triggers sound and turns the LEDs on. Conductive thread works as a cable for transferring electricity to the circuit, but can be also used as a pressure sensor. By connecting it to an analog pin of your Lilypad and observing the incoming values in the serial monitor of your Arduino sketch and then using these values to trigger some kind of behaviour, you can add interactivity to your circuit. So we decided to use this incoming values to trigger a sound function and added a speaker (the lilypad buzzer) in order to reproduce certain notes. The melody is from Kraftwerk’ s Transeurope Express anthem ;D

Source code:

int ledPin = 6; // LEDs are connected to digital pin 6
int speakerPin = 9; // speaker connected to digital pin 9
int sensorPin = 3; // conductive thread sensor is connected to analog pin 3
int sensorValue; // variable to store the value coming from the sensor

void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
Serial.begin(9600); // initialize the serial port
digitalWrite(17, HIGH); // sets analog pin a2 to high
}

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 {
scale(); // call the scale() function
delay(1000); // delay for 1 second
}
}

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

void scale () // melody function
{
digitalWrite(ledPin,HIGH); //turn on the LEDs
beep(speakerPin,196,500); //G 3
beep(speakerPin,207,500); //Ab 3
beep(speakerPin,277,450); //Db 4
beep(speakerPin,261,500); //C 4
beep(speakerPin,196,250); //G 3
beep(speakerPin,207,450); //Ab 3
beep(speakerPin,261,250); //C 4
beep(speakerPin,233,750); //Bb 3
beep(speakerPin,196,500); //G 3
beep(speakerPin,207,500); //Ab 3
beep(speakerPin,277,500); //Db 4
beep(speakerPin,261,500); //C 4
beep(speakerPin,233,250); //Bb 3
beep(speakerPin,261,750); //C 4
digitalWrite(ledPin,LOW); //turn off the LEDs
}