Can anyone help me? I'm trying to get my buzzer so that it buzzes when i use the bush buttons. For some reason, the buzzer keeps buzzing when its just plugged in but the noises do work when i push in the buttons. This is my code :
#define NOTE_G4 392
#define NOTE_B4 494
#define NOTE_A4 440
#define NOTE_D4 294
const int buttonrPin = 1; // pushbutton 1 pin
const int buttonyPin = 2; // pushbutton 2 pin
const int buttongPin = 3; // pushbutton 3 pin
const int buttonbPin = 4; //pushbutton 4 pin
const int buzzerPin = 13;
void setup() {
//inputs
pinMode(buttonrPin, INPUT);
pinMode(buttonyPin, INPUT);
pinMode(buttongPin, INPUT);
pinMode(buttonbPin, INPUT);
//output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int button1State, button2State, button3State, button4State;
button1State = digitalRead(buttonrPin);
button2State = digitalRead(buttonyPin);
button3State = digitalRead(buttongPin);
button4State = digitalRead(buttonbPin);
if (button1State == HIGH) {
tone(buzzerPin, NOTE_G4); }
else {
digitalWrite(buzzerPin, 0);
}
if (button2State == HIGH) {
tone(buzzerPin, NOTE_B4);}
else {
digitalWrite(buzzerPin, 0);}
if (button3State == HIGH) {
tone(buzzerPin, NOTE_A4);}
else {
digitalWrite(buzzerPin, 0); }
if (button4State == HIGH) {
tone(buzzerPin, NOTE_D4);}
else {
digitalWrite(buzzerPin, 0); }
}
Please help