#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define buzzer 6
int waktu;
int tombol1 = 2;
int tombol2 = 3;
int tombol3 = 4;
int tombol4 = 5;
bool tombol1_push;
bool tombol2_push;
bool tombol3_push;
bool tombol4_push;
int startstop;
int alarm;
int menit;
int detik;
void setup() {
// put your setup code here, to run once:
digitalWrite(buzzer, LOW);
alarm = 0;
startstop = 0;
waktu = 0;
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(tombol3, INPUT_PULLUP);
pinMode(tombol4, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Timer Alarm");
lcd.setCursor(10,2);
lcd.print("Menit");
lcd.setCursor(10,3);
lcd.print("Detik");
}
void loop() {
// put your main code here, to run repeatedly:
tombol1_push = digitalRead(tombol1);
tombol2_push = digitalRead(tombol2);
tombol3_push = digitalRead(tombol3);
tombol4_push = digitalRead(tombol3);
if (tombol2_push == 0){
if (startstop == 0){
startstop = 1;
}
else if (startstop == 1){
startstop = 0;
}
delay(1000);
}
if (tombol1_push == 0){
if (waktu >= 3600){
} else{
if (startstop == 0){
delay(250);
waktu = waktu + 10;
}
}
}
if (tombol4_push == 0){
if (waktu < 10){
} else{
if (startstop == 0){
waktu = waktu - 10;
delay(250);
}
}
}
if (waktu != 0){
if (startstop == 1){
waktu = waktu - 1;
delay(1000);
if (waktu == 0){
if (alarm == 0){
alarm = 1;
}
}
}
}
if (tombol3_push == 0){
alarm = 0;
startstop = 0;
noTone(buzzer);
}
if (alarm == 1){
tone(buzzer,4000);
delay(250);
noTone(buzzer);
}
menit = waktu / 60;
detik = waktu - (menit*60);
if (menit >= 10){
lcd.setCursor(1,2);
lcd.print(menit);
} else {
lcd.setCursor(1,2);
lcd.print("0");
lcd.setCursor(2,2);
lcd.print(menit);
}
if (detik >= 10){
lcd.setCursor(1,3);
lcd.print(detik);
} else {
lcd.setCursor(1,3);
lcd.print("0");
lcd.setCursor(2,3);
lcd.print(detik);
}
}