Skip to content

Instantly share code, notes, and snippets.

@upsilun
Created July 10, 2022 19:57
Show Gist options
  • Save upsilun/9f19b48cc6d345842f271586d4fc0736 to your computer and use it in GitHub Desktop.
Save upsilun/9f19b48cc6d345842f271586d4fc0736 to your computer and use it in GitHub Desktop.
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUZZER_PIN = 3; // Arduino pin connected to Buzzer's pin
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read new state
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(BUZZER_PIN, HIGH); // turn on
}
else if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(BUZZER_PIN, LOW); // turn off
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment