Hello guys i want to make a project with a buzzer with the code below but i can't because i get(when pressed). When not pressed works normally(it shows off only):
on
on
on
on
on
off
off
off
off
on
on
on
on
on
off
off
off
off
const int buzzer = 9; //buzzer to arduino pin 9
const int buttonPin = 4;
int buttonState = 0;
void setup(){
Serial.begin(9600);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
tone(buzzer, 5200);
delay(1);
tone(buzzer, 6100);
delay(1);
tone(buzzer, 9100);
Serial.println("on");
}else
{
tone(buzzer, 7300);
delay(1);
tone(buzzer, 10100);
delay(1);
tone(buzzer, 13000);
Serial.println("off");
}
}