# Problem with buzzer + flex sensor

Hello guys,

I am really new to this arduino thing but i’m loving it so far. So I came up with a project to work on but I’m having some serious problems now.

I’m making a glove that plays music. I am planning to put a flex sensor on each finger (except the thumb) and then assigning a note for each. There will be a push button connected so when its pushed the notes will change. So from the index finger to the pinky the notes will be “c”, “d”, “e” and “f”…and when the push button is pressed the notes will change to “g”, “a”, “b”, “C”.

So the basic idea here is to play music with your fingers. The flex sensors will sense that your fingers are bent and play a note according to that. I am also planning to attach LEDs on the tip of every finger for decoration purposes.

Anyway I started to write my code (as I said I’m pretty new to this) but its not going well. I combined different codes from the arduino site with my own code and this is what I got. So far I can’t even make one sensor to get a note play right. All I want is to sensors to play a note when they are bent and stop when they are back to their normal shape.

Here is where I got most of the code (and the notes) from: http://www.arduino.cc/en/Tutorial/PlayMelody

Here is the code:

#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz
// Define a special note, ‘R’, to represent a rest
#define R 0
int ledPin = 13; // LED connected to digital pin 13
int buzzer = 0;
int pushbt = 5;
int flexsensor1 = 1;
int flexsensor2 = 2;
int flexsensor3 = 3;
int flexsensor4 = 4;
int speakerOut=7;
int cNote = c;
int dNote = d;
int eNote = e;
int fNote = f;
int beat = 16;
int MAX_COUNT = 1; // Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int tone = 0;
long duration = 0;
void setup() // run once, when the sketch starts
{
pinMode(speakerOut, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
}
void loop() // run over and over again
{

delay(10);
delay(10);
delay(10);
Serial.print(“flexsensor1=”);
Serial.println(flexsensor1);
Serial.print(“flexsensor2=”);
Serial.println(flexsensor2);
Serial.print(“flexsensor3=”);
Serial.println(flexsensor3);
Serial.print(“flexsensor4=”);
Serial.println(flexsensor4);
if(flexsensor1<40){ //limit to 500
beat=beat+8;
play(cNote);
/digitalWrite(buzzer, HIGH);
delay(1);
digitalWrite(buzzer, LOW);

digitalWrite(ledPin, HIGH); // sets the LED on
delay(1);
digitalWrite(buzzer, LOW);
/
}
if(flexsensor1>40){
beat=16;

digitalWrite(ledPin, LOW);
}

if(flexsensor2<40){ //limit to 500
//play(dNote);
}
if(flexsensor2>40){

digitalWrite(ledPin, LOW);
}

if(flexsensor3<40){ //limit to 500
//play(eNote);
}
if(flexsensor3>40){

digitalWrite(ledPin, LOW);
}

if(flexsensor4<40){ //limit to 500
//play(fNote);
}
if(flexsensor4>40){

digitalWrite(ledPin, LOW);
}

}
void play(int note){
//for (int i=0; i<MAX_COUNT; i++) {
tone = note;

duration = beat * tempo; // Set up timing
playTone();
// A pause between notes…
delayMicroseconds(pause);
//}
}
void playTone() {
long elapsed_time = 0;
if (tone > 0) { // if this isn’t a Rest beat, while the tone has
// played less long than ‘duration’, pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
// DOWN
digitalWrite(speakerOut,LOW);
delayMicroseconds(tone / 2);
// Keep track of how long we pulsed
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}

Thank you very much!

how are you interfacing the flex sensors?

check my draw on flex sensor:

in my case, i get values within this range: 700-850.