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:

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
void loop() // run over and over again

flexsensor1 = analogRead(1);
flexsensor2 = analogRead(2);
flexsensor3 = analogRead(3);
flexsensor4 = analogRead(4);
if(flexsensor1<40){ //limit to 500
/digitalWrite(buzzer, HIGH);
digitalWrite(buzzer, LOW);

digitalWrite(ledPin, HIGH); // sets the LED on
digitalWrite(buzzer, LOW);

digitalWrite(ledPin, LOW);

if(flexsensor2<40){ //limit to 500

digitalWrite(ledPin, LOW);

if(flexsensor3<40){ //limit to 500

digitalWrite(ledPin, LOW);

if(flexsensor4<40){ //limit to 500

digitalWrite(ledPin, LOW);

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

duration = beat * tempo; // Set up timing
// A pause between notes…
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) {
delayMicroseconds(tone / 2);
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

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.