// Pin definitions
const int buttonPin1 = 7; // Pin untuk tombol A0
const int buttonPin2 = A1; // Pin untuk tombol A1
const int buttonPin3 = A2; // Pin untuk tombol A2
const int buzzerPin = 2; // Pin untuk buzzer
// Variables to store the state of the buttons
bool buttonState1 = false;
bool buttonState2 = false;
bool buttonState3 = false;
// Debounce time in milliseconds
const unsigned long debounceDelay = 50;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
// Function to read the button state with debouncing
bool readButton(int pin, bool &lastState, unsigned long &lastTime) {
bool currentState = digitalRead(pin);
if (currentState != lastState) {
lastTime = millis();
}
if ((millis() - lastTime) > debounceDelay) {
if (currentState != lastState) {
lastState = currentState;
}
}
return lastState;
}
void setup() {
// Initialize the pushbutton pins as inputs:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Initialize the buzzer pin as an output:
pinMode(buzzerPin, OUTPUT);
// Start with the buzzer off
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Read the state of the pushbuttons with debouncing
buttonState1 = readButton(buttonPin1, buttonState1, lastDebounceTime1);
buttonState2 = readButton(buttonPin2, buttonState2, lastDebounceTime2);
buttonState3 = readButton(buttonPin3, buttonState3, lastDebounceTime3);
// Check if any of the buttons is pressed
if (buttonState1 || buttonState2 || buttonState3) {
// Turn buzzer on
digitalWrite(buzzerPin, HIGH);
} else {
// Turn buzzer off
digitalWrite(buzzerPin, LOW);
}
}