I am working on a buzzer proj,the buzzer starts without the button being pressed

#include "pitches.h"
#define REST      0
const int sound = 6;
const int buzzer = 8;
const int halloween = 4;
const int starwars = 12;
int haloweenmelody[] = {
  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 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
};
int tempo = 120;
int melody[] = {
  NOTE_A4, -4, NOTE_A4, -4, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_F4, 8, REST, 8,
  NOTE_A4, -4, NOTE_A4, -4, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_F4, 8, REST, 8,
  NOTE_A4, 4, NOTE_A4, 4, NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16,

  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2, //4
  NOTE_E5, 4, NOTE_E5, 4, NOTE_E5, 4, NOTE_F5, -8, NOTE_C5, 16,
  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2,

  NOTE_A5, 4, NOTE_A4, -8, NOTE_A4, 16, NOTE_A5, 4, NOTE_GS5, -8, NOTE_G5, 16, //7
  NOTE_DS5, 16, NOTE_D5, 16, NOTE_DS5, 8, REST, 8, NOTE_A4, 8, NOTE_DS5, 4, NOTE_D5, -8, NOTE_CS5, 16,

  NOTE_C5, 16, NOTE_B4, 16, NOTE_C5, 16, REST, 8, NOTE_F4, 8, NOTE_GS4, 4, NOTE_F4, -8, NOTE_A4, -16, //9
  NOTE_C5, 4, NOTE_A4, -8, NOTE_C5, 16, NOTE_E5, 2,

  NOTE_A5, 4, NOTE_A4, -8, NOTE_A4, 16, NOTE_A5, 4, NOTE_GS5, -8, NOTE_G5, 16, //7
  NOTE_DS5, 16, NOTE_D5, 16, NOTE_DS5, 8, REST, 8, NOTE_A4, 8, NOTE_DS5, 4, NOTE_D5, -8, NOTE_CS5, 16,

  NOTE_C5, 16, NOTE_B4, 16, NOTE_C5, 16, REST, 8, NOTE_F4, 8, NOTE_GS4, 4, NOTE_F4, -8, NOTE_A4, -16, //9
  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2,

};
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
void setup(){
  pinMode(sound,INPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(halloween,INPUT);
  pinMode(starwars,INPUT);
}
void loop() {
  while (digitalRead(sound) == HIGH) {
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == HIGH) {
      for (int thisNote = 0; thisNote < 20; thisNote++) {

        int noteDuration = 1000 / noteDurations [thisNote];
        tone(8, haloweenmelody [thisNote], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(8);
      }
    }
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == HIGH) {
      for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
        divider = melody[thisNote + 1];
        if (divider > 0) {
          noteDuration = (wholenote) / divider;
        } else if (divider < 0) {
          noteDuration = (wholenote) / abs(divider);
          noteDuration *= 1.5;
        }
        tone(buzzer, melody[thisNote], noteDuration * 0.9);
        delay(noteDuration);
        noTone(buzzer);
      }
    }
    if (digitalRead(starwars) == HIGH && digitalRead(halloween) == LOW) {
      for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {


        divider = melody[thisNote + 1];
        if (divider > 0) {

          noteDuration = (wholenote) / divider;
        } else if (divider < 0) {
          // dotted notes are represented with negative durations!!
          noteDuration = (wholenote) / abs(divider);
          noteDuration *= 1.5;
        }


        tone(buzzer, melody[thisNote], noteDuration * 0.9);


        delay(noteDuration);


        noTone(buzzer);
      }
    }
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == LOW) {
      digitalWrite(buzzer,HIGH);
    }
  }
}

How is the button on pin 'buzzer' (6) wired?

FYI

just plugged nothing attached like a resistor or something

Wire the switch and use the code for S3 in the schematic.

i changed input to input_pullup even then the audio starts without the button being pressed and changed code from high to low

#include "pitches.h"
#define REST      0
const int sound = 6;
const int buzzer = 8;
const int halloween = 4;
const int starwars = 12;
int haloweenmelody[] = {
  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 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
};
int tempo = 120;
int melody[] = {
  NOTE_A4, -4, NOTE_A4, -4, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_F4, 8, REST, 8,
  NOTE_A4, -4, NOTE_A4, -4, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_A4, 16, NOTE_F4, 8, REST, 8,
  NOTE_A4, 4, NOTE_A4, 4, NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16,

  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2, //4
  NOTE_E5, 4, NOTE_E5, 4, NOTE_E5, 4, NOTE_F5, -8, NOTE_C5, 16,
  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2,

  NOTE_A5, 4, NOTE_A4, -8, NOTE_A4, 16, NOTE_A5, 4, NOTE_GS5, -8, NOTE_G5, 16, //7
  NOTE_DS5, 16, NOTE_D5, 16, NOTE_DS5, 8, REST, 8, NOTE_A4, 8, NOTE_DS5, 4, NOTE_D5, -8, NOTE_CS5, 16,

  NOTE_C5, 16, NOTE_B4, 16, NOTE_C5, 16, REST, 8, NOTE_F4, 8, NOTE_GS4, 4, NOTE_F4, -8, NOTE_A4, -16, //9
  NOTE_C5, 4, NOTE_A4, -8, NOTE_C5, 16, NOTE_E5, 2,

  NOTE_A5, 4, NOTE_A4, -8, NOTE_A4, 16, NOTE_A5, 4, NOTE_GS5, -8, NOTE_G5, 16, //7
  NOTE_DS5, 16, NOTE_D5, 16, NOTE_DS5, 8, REST, 8, NOTE_A4, 8, NOTE_DS5, 4, NOTE_D5, -8, NOTE_CS5, 16,

  NOTE_C5, 16, NOTE_B4, 16, NOTE_C5, 16, REST, 8, NOTE_F4, 8, NOTE_GS4, 4, NOTE_F4, -8, NOTE_A4, -16, //9
  NOTE_A4, 4, NOTE_F4, -8, NOTE_C5, 16, NOTE_A4, 2,

};
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
void setup(){
  pinMode(sound,INPUT_PULLUP);
  pinMode(buzzer,OUTPUT);
  pinMode(halloween,INPUT);
  pinMode(starwars,INPUT);
}
void loop() {
  while (digitalRead(sound) == LOW) {
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == HIGH) {
      for (int thisNote = 0; thisNote < 20; thisNote++) {

        int noteDuration = 1000 / noteDurations [thisNote];
        tone(8, haloweenmelody [thisNote], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(8);
      }
    }
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == HIGH) {
      for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
        divider = melody[thisNote + 1];
        if (divider > 0) {
          noteDuration = (wholenote) / divider;
        } else if (divider < 0) {
          noteDuration = (wholenote) / abs(divider);
          noteDuration *= 1.5;
        }
        tone(buzzer, melody[thisNote], noteDuration * 0.9);
        delay(noteDuration);
        noTone(buzzer);
      }
    }
    if (digitalRead(starwars) == HIGH && digitalRead(halloween) == LOW) {
      for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {


        divider = melody[thisNote + 1];
        if (divider > 0) {

          noteDuration = (wholenote) / divider;
        } else if (divider < 0) {
          // dotted notes are represented with negative durations!!
          noteDuration = (wholenote) / abs(divider);
          noteDuration *= 1.5;
        }


        tone(buzzer, melody[thisNote], noteDuration * 0.9);


        delay(noteDuration);


        noTone(buzzer);
      }
    }
    if (digitalRead(starwars) == LOW && digitalRead(halloween) == LOW) {
      digitalWrite(buzzer,HIGH);
    }
  }
}

sorry if there are dumb mistakes i am a beginner most of the code is copy and paste

Are these switches too ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.