#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int upButton = 35; // Pin untuk tombol up
const int downButton = 32; // Pin untuk tombol down
const int buzzerPin = 2; // Pin untuk buzzer
int counter = 0;
bool buzzerActive = false;
void setup() {
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Nyalakan backlight LCD
updateLCD(); // Tampilkan nilai awal counter
}
void loop() {
if (digitalRead(upButton) == LOW) {
delay(50);
if (digitalRead(upButton) == LOW) {
counter++;
updateLCD();
if (counter >= 10 && !buzzerActive) {
buzzerActive = true;
tone(buzzerPin, 1000);
}
while (digitalRead(upButton) == LOW);
}
}
if (digitalRead(downButton) == LOW) {
delay(50); // Debounce
if (digitalRead(downButton) == LOW) {
if (counter > 0) {
counter--;
}
updateLCD();
if (counter < 10 && buzzerActive) {
buzzerActive = false;
noTone(buzzerPin); // Matikan buzzer
}
while (digitalRead(downButton) == LOW);
}
}
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Counter: ");
lcd.print(counter);
lcd.setCursor(0, 1);
if (counter >= 10) {
lcd.print("Alarm Active!");
} else {
lcd.print("Alarm Off");
}
}