Buzzer + Flex Sensor problem (newb)

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
{

flexsensor1 = analogRead(1);
delay(10);
flexsensor2 = analogRead(2);
delay(10);
flexsensor3 = analogRead(3);
delay(10);
flexsensor4 = analogRead(4);
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!
Back to top

Here’s another sound code example that may help:
http://www.cs.colorado.edu/~buechley/LilyPad/07_sound_code.html

to get this code to work in your project, you’d just want to use a statement like this in loop():

if (flexsensor1>40)   //whatever your bent condition is...
{
   beep(speakerPin,2093,500);     //plays the note C (2093 Hz) for 500ms
}

hey thanks a lot for the reply. The code is working but its not doing what I want at the moment. I want it to play the note when I bend the flex sensor and stop when the flex sensor is back to its normal position. What the code is doing right now is that when I bend my flex sensor it plays the note for only a second or so and keeps repeating the beeping until I stop bending the sensor. What I want is a continuous sound not separate beeps (I hope u understand what I mean). Is it possible to make it play without introducing a delay? So that it play when the sensor is bent and stops when its normal again?

Thank you!

Oh and here is the new code if u want to see it:

#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 = 7;
int pushbt = 6;
int flexsensor1 = 1;
int flexsensor2 = 2;
int flexsensor3 = 3;
int flexsensor4 = 4;

void setup() // run once, when the sketch starts
{

pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
}

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

void loop() // run over and over again
{

flexsensor1 = analogRead(1);
delay(10);
flexsensor2 = analogRead(2);
delay(10);
flexsensor3 = analogRead(3);
delay(10);
flexsensor4 = analogRead(4);
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)
{
beep(buzzer,2093,500); //plays the note C (2093 Hz) for 500ms

//the other flex sensors will be added
}

Ok guys I refined my code a bit more and added the 4 LEDs + the Push Button into the code. The push button and the flex sensors are working together nicely right now so I have all the 8 notes working.

Though I still need the sound coming out of the buzzer to be continues instead of just 1 second beeps.

Here is a more refined version of my code:

/* ------------ Defining the notes ------------ */

#define  c     3830    // 261 Hz 
#define  cs    3615    // c-sharp / d-flat
#define  d     3400    // 294 Hz 
#define  ds    3219    // d-sharp / e-flat
#define  e     3038    // 329 Hz 
#define  es    2951    // e-sharp / f-flat
#define  f     2864    // 349 Hz 
#define  fs    2707    // f-sharp / g-flat
#define  g     2550    // 392 Hz 
#define  gs    2411    // g-sharp / a-flat
#define  a     2272    // 440 Hz 
#define  as    2150    // a-sharp / b-flat
#define  b     2028    // 493 Hz 
#define  bs    1970    // b-sharp / C-flat
#define  C     1912    // 523 Hz 


int ledPin1 = 13;// LED connected to digital pin 13
int ledPin2 = 4;
int ledPin3 = 3;
int ledPin4 = 2;
int buzzer = 7;
int pushbt = 6;
int flexsensor1 = 1;
int flexsensor2 = 2;
int flexsensor3 = 3;
int flexsensor4 = 4;
int val = 0;


void setup()                    // run once, when the sketch starts
{
   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(buzzer, OUTPUT);  // sets the digital pin as output
  pinMode(pushbt, INPUT);
  Serial.begin(9600);
}

void beep (unsigned char buzzer, 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(buzzer,HIGH);
              delayMicroseconds(delayAmount);
              digitalWrite(buzzer,LOW);
              delayMicroseconds(delayAmount);
          }        
}

void loop()                     // run over and over again
{
 
flexsensor1 = analogRead(1); 
delay(10);
flexsensor2 = analogRead(2);  
delay(10);
flexsensor3 = analogRead(3);  
delay(10);
flexsensor4 = analogRead(4);  
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);

val = digitalRead(pushbt);

/* ------------ notes "c" and "g" + LED 1 ------------ */

if (flexsensor1<40&&val == LOW)   
{
   beep(buzzer,c,500);     //plays the note c
   digitalWrite(ledPin1, HIGH);   
}

if (flexsensor1<40&&val == HIGH)   
{
   beep(buzzer,g,500);     //plays the note g
   digitalWrite(ledPin1, HIGH);   
}

/* ------------ notes "d" and "a" + LED 1 ------------ */

if (flexsensor2<40&&val == LOW)   
{
   beep(buzzer,d,500);     //plays the note d
   digitalWrite(ledPin2, HIGH);   
}
if (flexsensor2<40&&val == HIGH)   
{
   beep(buzzer,a,500);     //plays the note a
   digitalWrite(ledPin2, HIGH);   
}

/* ------------ notes "e" and "b" + LED 1 ------------ */

if (flexsensor3<40&&val == LOW)   
{
   beep(buzzer,e,500);     //plays the note e
   digitalWrite(ledPin3, HIGH);   
}
if (flexsensor3<40&&val == HIGH)   
{
   beep(buzzer,b,500);     //plays the note b
   digitalWrite(ledPin3, HIGH);   
}

/* ------------ notes "f" and "C" + LED 1 ------------ */

if (flexsensor4<10&&val == LOW)   
{
   beep(buzzer,f,500);     //plays the note f
   digitalWrite(ledPin4, HIGH);   
}
if (flexsensor4<10&&val == HIGH)   
{
   beep(buzzer,C,500);     //plays the note C 
   digitalWrite(ledPin4, HIGH);   
}
}

Oh and now I’m trying to add the sharp notes. So when flexsensor1 and flexsensor2 are bent together at the same time a c-sharp will be played. I defined the sharp notes in my code (previous post) but I dont know how to get that running. I tried this but it plays “c” then “c-sharp” and then “d”:

/* ------------ notes "c" and "g" + LED 1 ------------ */

if (flexsensor1<40&&val == LOW)   
{
   beep(buzzer,c,500);     //plays the note c
   digitalWrite(ledPin1, HIGH);   
}

if (flexsensor1<40&&val == HIGH)   
{
   beep(buzzer,g,500);     //plays the note g
   digitalWrite(ledPin1, HIGH);   
}

/* ------------ notes "c-sharp + LED 1 ------------ */

if (flexsensor1<40&&flexsensor2<40&&val == LOW)   
{
   beep(buzzer,cs,500);     //plays the note cs
   digitalWrite(ledPin1, HIGH);   
}

if (flexsensor1<40&&val == HIGH)   
{
   beep(buzzer,g,500);     //plays the note g
   digitalWrite(ledPin1, HIGH);   
}

/* ------------ notes "d" and "a" + LED 1 ------------ */

if (flexsensor2<40&&val == LOW)   
{
   beep(buzzer,d,500);     //plays the note d
   digitalWrite(ledPin2, HIGH);   
}
if (flexsensor2<40&&val == HIGH)   
{
   beep(buzzer,a,500);     //plays the note a
   digitalWrite(ledPin2, HIGH);   
}

you’ve got all the tools ;-)… note that the last variable of the beep function is the length of time of the beep. so, this will almost get you there:

while (condition)
{
    beep(buzzer,C,1);     //plays the note C *FOR 1 ms*
}

& to get really continuous sound, try something like this instead:

if (condition)
{
  set your note/frequency info here
  while (condition)
  {
     do the high/low pulsing on the buzzer pin here
  }
}

hey thanks for the reply. I tried changing the length to 1ms but it didn’t work as I wanted. The buzzer only made ticking noises.

I want to try the second thing you suggested but I’m afraid I’m having trouble understanding it.

Here’s what I’ve done with the notes (latest version - still need continuous sound):

void loop()                     // run over and over again
{
 
flexsensor1 = analogRead(1);     // read the flex sensors
delay(10);
flexsensor2 = analogRead(2);  
delay(10);
flexsensor3 = analogRead(3);  
delay(10);
flexsensor4 = analogRead(4);  
delay(10);
Serial.print("flexsensor1=");    // print the readings from the flex sensors       
Serial.println(flexsensor1);
Serial.print("flexsensor2=");           
Serial.println(flexsensor2);
Serial.print("flexsensor3=");           
Serial.println(flexsensor3);
Serial.print("flexsensor4=");           
Serial.println(flexsensor4);

val = digitalRead(pushbt);     // read input value

/* ------------ notes "c" and "g" + LED 1 ------------ */

if (flexsensor1<40&&flexsensor2>35&&flexsensor2>40&&flexsensor2>10&&val == LOW)    // when both the flex sensor and the push button is pressed at the same time   
{digitalWrite(ledPin1, HIGH); 
   beep(buzzer,c,500);     //plays the note c
     // turn LED ON
   
   digitalWrite(ledPin1, LOW);
}
  
if (flexsensor1<40&&flexsensor2>35&&flexsensor2>40&&flexsensor2>10&&val == HIGH)   
{   digitalWrite(ledPin1, HIGH); 
   beep(buzzer,g,500);     //plays the note g
  
 
   digitalWrite(ledPin1, LOW);
}

/* ------------ notes "c-sharp" and "g-sharp" + LED 1 and 2 ------------ */

if (flexsensor1<40&&flexsensor2<35&&flexsensor3>40&&flexsensor4>10&&val == LOW)   
{ digitalWrite(ledPin1, HIGH); 
   digitalWrite(ledPin2, HIGH);  
   beep(buzzer,cs,500);     //plays the note cs
  
  
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin1, LOW);
}
if (flexsensor1<40&&flexsensor2<35&&flexsensor3>40&&flexsensor4>10&&val == HIGH)   
{
   digitalWrite(ledPin1, HIGH);
   digitalWrite(ledPin2, HIGH);   
  beep(buzzer,gs,500);     //plays the note gs
  
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
}

/* ------------ notes "d" and "a" + LED 2 ------------ */

if (flexsensor2<35&&flexsensor1>40&&flexsensor3>40&&flexsensor4>10&&val == LOW)   
{
    digitalWrite(ledPin2, HIGH); 
   beep(buzzer,d,500);     //plays the note d
   digitalWrite(ledPin2, LOW);
}
if (flexsensor2<35&&flexsensor1>40&&flexsensor3>40&&flexsensor4>10&&val == HIGH)   
{  digitalWrite(ledPin2, HIGH); 
   beep(buzzer,a,500);     //plays the note a
   
   digitalWrite(ledPin2, LOW);
}

/* ------------ notes "d-sharp" and "a-sharp" + LED 2 and 3 ------------ */

if (flexsensor2<35&&flexsensor3<40&&flexsensor1>40&&flexsensor4>10&&val == LOW)   
{
   digitalWrite(ledPin2, HIGH);
   digitalWrite(ledPin3, HIGH);   
   beep(buzzer,ds,500);     //plays the note ds
   
   digitalWrite(ledPin2, LOW);
   digitalWrite(ledPin3, LOW);
}

if (flexsensor2<35&&flexsensor3<40&&flexsensor1>40&&flexsensor4>10&&val == HIGH)   
{ digitalWrite(ledPin2, HIGH);   
   digitalWrite(ledPin3, HIGH);
   beep(buzzer,as,500);     //plays the note as
  
   digitalWrite(ledPin2, LOW);
   digitalWrite(ledPin3, LOW);
}

/* ------------ notes "e" and "b" + LED 3 ------------ */

if (flexsensor3<40&&flexsensor1>40&&flexsensor2>35&&flexsensor4>10&&val == LOW)   
{   digitalWrite(ledPin3, HIGH);
   beep(buzzer,e,500);     //plays the note e
   
   digitalWrite(ledPin3, LOW);
}
if (flexsensor3<40&&flexsensor1>40&&flexsensor2>35&&flexsensor4>10&&val == HIGH)   
{  digitalWrite(ledPin3, HIGH);   
   beep(buzzer,b,500);     //plays the note b
   
  
   digitalWrite(ledPin3, LOW);
}

/* ------------ notes "b-sharp" and "f-sharp" + LED 3 and 4 ------------ */

if (flexsensor3<40&&flexsensor4<10&&flexsensor1>40&&flexsensor2>35&&val == HIGH)   
{
   digitalWrite(ledPin3, HIGH);
   digitalWrite(ledPin4, HIGH);   
   beep(buzzer,bs,500);     //plays the note bs
   
   digitalWrite(ledPin3, LOW);
   digitalWrite(ledPin4, LOW);
}
if (flexsensor3<40&&flexsensor4<10&&flexsensor1>40&&flexsensor2>35&&val == LOW)   
{  digitalWrite(ledPin3, HIGH);
   digitalWrite(ledPin4, HIGH);
   beep(buzzer,fs,500);     //plays the note fs
   
   digitalWrite(ledPin3, LOW);
   digitalWrite(ledPin4, LOW);
}

/* ------------ notes "f" and "C" + LED 4 ------------ */

if (flexsensor4<10&&flexsensor1>40&&flexsensor2>35&&flexsensor3>40&&val == LOW)   
{    digitalWrite(ledPin4, HIGH);   
   beep(buzzer,f,500);     //plays the note f
   
   digitalWrite(ledPin4, LOW);
}
if (flexsensor4<10&&flexsensor1>40&&flexsensor2>35&&flexsensor3>40&&val == HIGH)   
{
   digitalWrite(ledPin4, HIGH);   
   beep(buzzer,C,500);     //plays the note C 
    
   digitalWrite(ledPin4, LOW);
}
}