Problems with code for piezo buzzer & push button

Hi,

I want to combine a piezo speaker with a push button, so when you push the button, you can hear a song coming from the piezo speaker (one time). I have a code, but there’s something not right, because it’s not working. Any advise?

int speakerPin = 2;
int inPin = 4;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

boolean isPushed = false; // variable for the status of the button
boolean isPressed = false; // variable for the status of the piezo
boolean oncePressed = false;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);       // set up the pathway to write on your monitor
}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void loop(){
  val = digitalRead(inPin);  // read input value
   Serial.println(val);      // print the value of "value" on the Serial Monitor
  if (val == HIGH) {         // check if the input is HIGH (button pushed)
    oncePressed = true;
    isPushed = true;
  } else {
    if(isPushed){
       if(isPressed){
            digitalWrite(speakerPin, LOW);  // turn music OFF
            stopMuziek();
            isPressed = false;
        } else{
         if(oncePressed){
             digitalWrite(speakerPin, HIGH);  // turn music ON
             speelMuziek();
             isPressed = true;
          }
        }
        isPushed = false;
    } else {
    }
  }
}

void stopMuziek(){
    noTone(5);
}
void speelMuziek(){
    for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
    // pause between notes
    delay(tempo / 2); 
  }
   stopMuziek();
}

but there’s something not right, because it’s not working.

That code does something.
You want it to do something.

Clearly what it does is not what you want. If they were, you wouldn’t have a problem.

But, what either one of those somethings is is a mystery.

  pinMode(inPin, INPUT);    // declare pushbutton as input

No use of the internal pullup resistor, so that means you are using an external pullup or pulldown resistor. You are, aren’t you? Why not use the internal pullup?

Your logic behind what to do when the switch is pressed is far too complicated. It needs some comments to describe what you were thinking. Or, it needs simplifying.

If the piezo is being used to make music, it is an output device. The isPressed variable makes no sense for an output device.

No, inPin is the pin for the push button. As for the rest you said, I have no clue what you mean. I just want my piezo speaker to play a song when you push on the button, but I'm not a coding expert and apparently I did do something wrong with the code.

At the moment, when I push the button, nothing happens.

Something like the following may work for you:

int c = 1915;  //define note C as 1915Hz
int d = 1700;
int e = 1519;

int tune[] = {c,d,d,0,e,c,e,d}; //define the order of notes to play


for(int i = 0; i < sizeof(tune); i++){    // do as many times as the tune[] array is long
  tone(speakerPin, tune[i],200);          // use the Tone() function to make sounds for 200milliseconds
 delay(200);      // wait 200ms before next note
}

edit: sorry, threw that together on my lunch-hour and was interrupted. this actually compiles now…