Halloween and Star wars on two push botton and buzzer

When I click on the button, it just won’t stop. It even start before I press the button.
please help “error code”
Thankyou

/*
  Two melodies on two buttons and one speaker

  Plays a melody determined by button pushed

  circuit:
  - 8 ohm speaker on digital pin 8
  - push button on digital pin 2
  - push button on digital pin 7

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"



// notes in the melody:
int halloweenMelody[] = {
  NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4
};

int starwarsMelody[] = {
  NOTE_G3, NOTE_G3, NOTE_G3, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_D4
};

int halloween = 2;
int starwars = 7;

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};

int counterDurations[] = {
  6, 6, 6, 1, 1, 6, 6, 6, 1, 2, 6, 6, 6, 1, 2, 6, 6, 6, 1
};

void setup(){
  //make the button's pin input
  pinMode(halloween, INPUT);
  pinMode(starwars, INPUT);
}

void loop() {


  //if the button is pressed
  if (digitalRead(halloween) == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <20; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / noteDurations [thisNote];
      tone(8, halloweenMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    } }
 

  //if the button is pressed
  if (digitalRead(starwars) == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <19; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / counterDurations [thisNote];
      tone(8, starwarsMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    }}}

Since you did not specify INPUT_PULLUP for the buttons do you have a pulldown resistor included in your circuit?

Is your button wired to + or GND? The simplest way to wire a button is to wire it to GND, specify INPUT_PULLUP for the pinMode, and check for a HIGH to LOW transition to detect the button being pressed.

My button is hooked up to GND and i just can't seem to stop the music, both. Whenever I turn to Starwars music, it play once and turn back to Halloween music, and where do I put the INPUT_PULLUP?

Configure buttons for INPUT_PULLUP:

void setup(){
  //make the button's pin input
  pinMode(halloween, INPUT_PULLUP);
  pinMode(starwars, INPUT_PULLUP);
}

Since your button is wired to GND the input will be LOW when the button is pressed:

  if (digitalRead(halloween) == 1) {

should be

  if (digitalRead(halloween) == LOW) {

and

  if (digitalRead(starwars) == 1) {

should be

  if (digitalRead(starwars) == LOW) {

I did change what you asked, but It still doesn’t stop, can anyone check this code?

/*
  Two melodies on two buttons and one speaker

  Plays a melody determined by button pushed

  circuit:
  - 8 ohm speaker on digital pin 8
  - push button on digital pin 2
  - push button on digital pin 7

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"



// notes in the melody:
int halloweenMelody[] = {
  NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4
};

int starwarsMelody[] = {
  NOTE_G3, NOTE_G3, NOTE_G3, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_D4
};

int halloween = 2;
int starwars = 7;

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};

int counterDurations[] = {
  6, 6, 6, 1, 1, 6, 6, 6, 1, 2, 6, 6, 6, 1, 2, 6, 6, 6, 1
};

void setup(){
  //make the button's pin input
  pinMode(halloween, INPUT_PULLUP);
  pinMode(starwars, INPUT_PULLUP);
}

void loop() {


  //if the button is pressed
  if (digitalRead(halloween) == LOW) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <20; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / noteDurations [thisNote];
      tone(8, halloweenMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    } }
 

  //if the button is pressed
  if (digitalRead(starwars) == LOW) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <19; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / counterDurations [thisNote];
      tone(8, starwarsMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    }}}

Post your wiring diagram. I can't see why it won't work. The easiest way to debug would be to take out the music and print to serial to serial monitor what you are reading from each pin. When you are able to read pins properly then put the music back in. Like this:

const int halloween = 2;
const int starwars = 7;

void setup(){
  pinMode(halloween, INPUT_PULLUP);
  pinMode(starwars, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {

  if (digitalRead(halloween) == LOW)
  {
      Serial.println("halloween is LOW");
  }
  else
  {
      Serial.println("halloween is HIGH");
  }
  if (digitalRead(starwars) == LOW)
  {
      Serial.println("starwars is LOW");
  }
  else
  {
      Serial.println("starwars is HIGH");
  }
  delay(250);
}

I added the 250ms delay so you would have so many prints.

Yes, I got it to work, Thank you very much

You’re welcome!