#include <LiquidCrystal.h> //
#include <Button.h> //
#include <DS3231.h> // RTC
int buzzer = 13; //set . 13
uint8_t hh = 0, mm = 0, ss = 0; //set ,, RTC
uint8_t timerMode = 0 ,setMode=0, setAlarm=0, alarmMode=0 ; //set
uint8_t alarmHH =0 ,alarmMM =0 ,alarmSS=0; //
uint8_t timerhh=0, timermm=0, timerss =0; //
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD
DS3231 rtc(SDA,SCL); // RTC
Time t; // t
#define DN_PIN 7 //set pin
#define UP_PIN 8 //set pin
#define SET_PIN 9 //set
#define ALR_PIN 10 //set
#define PULLUP true //aktifkan
#define INVERT true //If invert ==true,HIGH=released,LOW=pressed
#define DEBOUNCE_MS 20 //state react with button
#define REPEAT_FIRST 500 //state untuk tekan berapa lama button
#define REPEAT_INCR 100 // increase kan number
//Declare push buttons
Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS); //set 1st button utk decrement
Button btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS); //set 2nd button utk increment
Button btnSET(SET_PIN, PULLUP, INVERT, DEBOUNCE_MS); //set button utk function timer
Button btnALR(ALR_PIN, PULLUP, INVERT, DEBOUNCE_MS); //set button utk function alarm
enum {WAIT, INCR, DECR}; //The possible states for the state machine
uint8_t STATE; //The current state machine state
int count; //The number that is adjusted
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the sketch starts)
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long presses
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(buzzer,OUTPUT);
rtc.begin();
lcd.setCursor(0,0);
lcd.print("Welcome Shaqib!");
delay(2000);
lcd.clear();
//rtc.setTime(12,24,00);
}
void loop() {
t=rtc.getTime();
hh=t.hour,DEC;
mm=t.min,DEC;
ss=t.sec,DEC;
btnSET.read();
btnUP.read();
btnDN.read();
btnALR.read();
if(setMode==0 && setAlarm==0 ){
tunjukJamTemp();
tunjukTimer();}
if(setMode!=0 && setAlarm==0 && alarmMode==0 ){delay(100);}
if(setMode==1 && setAlarm==0 && alarmMode==0 ){lcd.setCursor(7,1);lcd.print(" ");delay(100);tunjukJamTemp();tunjukTimer();}
if(setMode==2 && setAlarm==0 && alarmMode==0 ){lcd.setCursor(10,1);lcd.print(" ");delay(100);tunjukJamTemp();tunjukTimer();}
if(setMode==3 && setAlarm==0 && alarmMode==0 ){lcd.setCursor(13,1);lcd.print(" ");delay(100);tunjukJamTemp();tunjukTimer();}
if(setMode==4 && setAlarm==0 && alarmMode==0 ){tunjukJamTemp();tunjukTimer();stepDown();delay(1000);}
if(setMode==0 && setAlarm!=0 && alarmMode==0 ){delay(100);}
if(setMode==0 && setAlarm==2 && alarmMode==0 ){lcd.setCursor(0,0);lcd.print(" ");delay(100); lcd.setCursor(0,0);
if(alarmHH<10){lcd.print("0");}
lcd.print(alarmHH);lcd.print(":");
if(alarmMM<10){lcd.print("0");}
lcd.print(alarmMM);lcd.print(":");
if(alarmSS<10){lcd.print("0");}
lcd.print(alarmSS);lcd.setCursor(0,1);
lcd.print("Set Your Alarm");}
if(setMode==0 && setAlarm==3 && alarmMode==0 ){lcd.setCursor(3,0);lcd.print(" ");delay(100); lcd.setCursor(0,0);
if(alarmHH<10){lcd.print("0");}
lcd.print(alarmHH);lcd.print(":");
if(alarmMM<10){lcd.print("0");}
lcd.print(alarmMM);lcd.print(":");
if(alarmSS<10){lcd.print("0");}
lcd.print(alarmSS);lcd.setCursor(0,1);
lcd.print("Set Your Alarm");}
if(setMode==0 && setAlarm==4 && alarmMode==0 ){lcd.setCursor(6,0);lcd.print(" ");delay(100); lcd.setCursor(0,0);
if(alarmHH<10){lcd.print("0");}
lcd.print(alarmHH);lcd.print(":");
if(alarmMM<10){lcd.print("0");}
lcd.print(alarmMM);lcd.print(":");
if(alarmSS<10){lcd.print("0");}
lcd.print(alarmSS);lcd.setCursor(0,1);
lcd.print("Set Your Alarm");}
if(setMode==0 && setAlarm==5 && alarmMode==0 ){alarmMode=1;setAlarm=0;}
setupAlarm();
Alarm(alarmHH,alarmMM);
btnSET.read();
btnUP.read();
btnDN.read();
btnALR.read();
switch (STATE) {
case WAIT:
if (btnSET.wasPressed())
{ setMode = setMode+1;}
if (btnALR.wasPressed())
{ setAlarm = setAlarm+1;}
if (btnUP.wasPressed())
STATE = INCR;
else if (btnDN.wasPressed())
STATE = DECR;
else if (btnUP.wasReleased())
rpt = REPEAT_FIRST;
else if (btnDN.wasReleased())
rpt = REPEAT_FIRST;
else if (btnUP.pressedFor(rpt)) {
rpt += REPEAT_INCR;
STATE = INCR;
}
else if (btnDN.pressedFor(rpt)) {
rpt += REPEAT_INCR;
STATE = DECR;
}
break;
case INCR:
if (setMode==1 && setAlarm==0 && alarmMode==0 && timerhh<23)timerhh=timerhh+1;
if (setMode==2 && setAlarm==0 && alarmMode==0 && timermm<59)timermm=timermm+1;
if (setMode==3 && setAlarm==0 && alarmMode==0 && timerss<59)timerss=timerss+1;
if (setMode==0 && setAlarm==2 && alarmMode==0 && alarmHH<23)alarmHH=alarmHH+1;
if (setMode==0 && setAlarm==3 && alarmMode==0 && alarmMM<59)alarmMM=alarmMM+1;
if (setMode==0 && setAlarm==4 && alarmMode==0 && alarmSS<59)alarmSS=alarmSS+1;
STATE = WAIT;
break;
case DECR:
if (setMode==1 && setAlarm==0 && alarmMode==0 && timerhh>0)timerhh=timerhh-1;
if (setMode==2 && setAlarm==0 && alarmMode==0 && timermm>0)timermm=timermm-1;
if (setMode==3 && setAlarm==0 && alarmMode==0 && timerss>0)timerss=timerss-1;
if (setMode==0 && setAlarm==2 && alarmMode==0 && alarmHH>0)alarmHH=alarmHH-1;
if (setMode==0 && setAlarm==3 && alarmMode==0 && alarmMM>0)alarmMM=alarmMM-1;
if (setMode==0 && setAlarm==4 && alarmMode==0 && alarmSS>0)alarmSS=alarmSS-1;
STATE = WAIT;
break;
}
}
void stepDown() {
if (timerss > 0) {
timerss -= 1;
} else {
if (timermm > 0) {
timerss = 59;
timermm -= 1;
} else {
if (timerhh > 0) {
timerss = 59;
timermm = 59;
timerhh -= 1;
} else {
for(int i=0;i<30;i++){
lcd.setCursor(0,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(10,0);
lcd.print(rtc.getTemp());
lcd.print("C");
btnSET.read();
if(btnSET.wasPressed()){digitalWrite(buzzer,LOW);i=30;}else{
digitalWrite(buzzer,HIGH);
delay(500);
digitalWrite(buzzer,LOW);
delay(500);}
}
setMode=0;
}
}
}
}
void tunjukTimer(){
lcd.setCursor(0, 1);
lcd.print("Timer:");
lcd.print(" ");
(timerhh < 10) ? lcd.print("0") : NULL;
lcd.print(timerhh);
lcd.print(":");
(timermm < 10) ? lcd.print("0") : NULL;
lcd.print(timermm);
lcd.print(":");
(timerss < 10) ? lcd.print("0") : NULL;
lcd.print(timerss);
}
void tunjukJamTemp(){
lcd.setCursor(0,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(10,0);
lcd.print(rtc.getTemp());
lcd.print("C");
}
void Alarm(uint8_t alarmHH,uint8_t alarmMM){
if(alarmMode==1 && alarmHH==hh && alarmMM==mm){
for(int i=0;i<30;i++){
lcd.setCursor(0,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(10,0);
lcd.print(rtc.getTemp());
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Timer:");
lcd.print(" ");
(timerhh < 10) ? lcd.print("0") : NULL;
lcd.print(timerhh);
lcd.print(":");
(timermm < 10) ? lcd.print("0") : NULL;
lcd.print(timermm);
lcd.print(":");
(timerss < 10) ? lcd.print("0") : NULL;
lcd.print(timerss);
btnALR.read();
if(btnALR.wasPressed()){digitalWrite(buzzer,LOW);i=30;}else{
digitalWrite(buzzer,HIGH);
delay(500);
digitalWrite(buzzer,LOW);
delay(500);}
}
alarmMode=0;
setAlarm=0;
}}
void setupAlarm(){
if(setMode==0 && setAlarm==1 && alarmMode==0 ){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Set Your Alarm");
lcd.setCursor(0,0);
if(alarmHH<10){lcd.print("0");}
lcd.print(alarmHH);lcd.print(":");
if(alarmMM<10){lcd.print("0");}
lcd.print(alarmMM);lcd.print(":");
if(alarmSS<10){lcd.print("0");}
lcd.print(alarmSS);
}
}
Comments