//Name : Making Sound of the Buzzer with Push Button Control
//Libraries
//Define Pins
const int buttonPin = 2; //Pin connected to the push button
const int buzzerPin = 8; //Pin connected to the buzzer
//Variables
void setup() {
//Set pins as Inputs or Outputs
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
//Read the push button state
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
tone(buzzerPin , 262); //Turn the buzzer ON
buttonState = digitalRead(buttonPin);
}
else {
noTone(buzzerPin); //Turn the buzzer OFF
buttonState = digitalRead(buttonPin);
}
}