Last active
November 21, 2018 02:44
-
-
Save schappim/d9a38bee51d8c1c4881ab1aeb97f71c4 to your computer and use it in GitHub Desktop.
Buzzer Push Button Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// constants won't change. They're used here to set pin numbers: | |
const int buttonPin = 2; // the number of the pushbutton pin | |
const int button1Pin = 3; // the number of the pushbutton pin | |
const int ledPin = 13; // the number of the LED pin | |
const int buzzerPin = 9; // buzzer to arduino pin 9 | |
// variables will change: | |
int buttonState = 0; // variable for reading the pushbutton status | |
int button1State = 0; // variable for reading the pushbutton status | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(ledPin, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(buttonPin, INPUT); | |
pinMode(button1Pin, INPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(buzzerPin, OUTPUT); | |
} | |
void loop() { | |
// read the state of the pushbutton value: | |
buttonState = digitalRead(buttonPin); | |
button1State = digitalRead(button1Pin); | |
// check if the pushbutton is pressed. If it is, the buttonState is HIGH: | |
if (buttonState == HIGH) { | |
// turn LED on: | |
digitalWrite(ledPin, HIGH); | |
} else { | |
// turn LED off: | |
digitalWrite(ledPin, LOW); | |
} | |
// check if the other pushbutton is pressed. If it is, the buttonState is HIGH: | |
if (button1State == HIGH) { | |
// turn Buzzer on: | |
tone(buzzerPin, 1000); | |
} else { | |
// turn Buzzer off: | |
noTone(buzzerPin); // Stop sound | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment