#include <LiquidCrystal_I2C.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
#define Bt_SetAlarm A0
#define Bt_Down A1
#define Bt_Up A2
#define Bt_SetTime A3
#define Buzzer 8
unsigned long t_0 = 0;
bool blinkState = false;
//*********** BUTTON STATE VARIABLEs ************************************************************************//
int Bt_Alarm_State = 1;
int Bt_Down_State = 1;
int Bt_Up_State = 1;
int Bt_SetTime_State = 1;
//***********************************************************************************************************//
int HH, MM, ss, DD, Day, MMMM, YYYY; //*********** TIME & DATE VARIABLEs **************//
int NewHH, NewMM, Newss, NewDD, NewDay, NewMMMM, NewYYYY; //*********** NEW TIME & DATE VARIABLEs **********//
int AHH = -1, AMM = -1;
int mode = 0;
int Cursor1 = 0; // ********** Time Cursor ******************************************************************//
int Cursor2 = 0; // ********** Alarm Cursor *****************************************************************//
String dayOfWeek[] = { "Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat" }; // Day of week ********************//
//0x3F or 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2); //LCD Object
ThreeWire myWire(11, 10, 12); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire); // RTC Object
//*************************************************************************************//
//********************************* Main SetUp ****************************************//
//*************************************************************************************//
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
Rtc.Begin();
//RtcDateTime currentTime = RtcDateTime(__DATE__ , __TIME__);
//Rtc.SetDateTime(currentTime);
pinMode(Bt_SetAlarm, INPUT_PULLUP);
pinMode(Bt_Down, INPUT_PULLUP);
pinMode(Bt_Up, INPUT_PULLUP);
pinMode(Bt_SetTime, INPUT_PULLUP);
pinMode(Buzzer, OUTPUT);
}
//*************************************************************************************//
//********************************* Main Loop *****************************************//
//*************************************************************************************//
void loop() {
RtcDateTime now = Rtc.GetDateTime();
// Assigning Varibles
HH = now.Hour();
MM = now.Minute();
ss = now.Second();
DD = now.Day();
Day = now.DayOfWeek();
MMMM = now.Month();
YYYY = now.Year();
HH = 12;
MM = 12;
ss = 12;
DD = 1;
Day = 1;
MMMM = 12;
YYYY = 2012;
ReadInput();
// **************** HOME MODE ***************//
if (Cursor1 == 0 && Cursor2 == 0) {
HomeView();
}
// *****************************************//
// Time SETTING ***************************************************************************************//
if (Bt_SetTime_State == 0) {
Cursor1++;
delay(200);
NewHH = HH;
NewMM = MM;
Newss = ss;
NewDD = DD;
NewMMMM = MMMM;
NewYYYY = YYYY;
//lcd.clear();
}
while (Cursor1 == 1 || Cursor1 == 2 || Cursor1 == 3 || Cursor1 == 4 || Cursor1 == 5 || Cursor1 == 6) {
ReadInput();
SetTime();
if (Bt_SetTime_State == 0) {
Cursor1++;
delay(200);
if (Cursor1 > 6) {
Cursor1 = 0;
UdateTime();
}
}
if (Cursor1 == 1) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(4, 1);
if (blinkState) {
lcd.print(" ");
} else {
if (NewHH < 10) lcd.print("0");
lcd.print(NewHH);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
if (Cursor1 == 2) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(7, 1);
if (blinkState) {
lcd.print(" ");
} else {
if (NewMM < 10) lcd.print("0");
lcd.print(NewMM);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
if (Cursor1 == 3) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(10, 1);
if (blinkState) {
lcd.print(" ");
} else {
if (Newss < 10) lcd.print("0");
lcd.print(Newss);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
if (Cursor1 == 4) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(5, 0);
if (blinkState) {
lcd.print(" ");
} else {
if (NewDD < 10) lcd.print("0");
lcd.print(NewDD);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
if (Cursor1 == 5) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(8, 0);
if (blinkState) {
lcd.print(" ");
} else {
if (NewMMMM < 10) lcd.print("0");
lcd.print(NewMMMM);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
if (Cursor1 == 6) {
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(11, 0);
if (blinkState) {
lcd.print(" ");
} else {
lcd.print(NewYYYY);
}
t_0 = t_now;
blinkState = !blinkState; // Toggle blink state
}
}
else{
UdateTime();
HomeView();
unsigned long t_now = millis();
if (t_now - t_0 >= 500) {
lcd.setCursor(5, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
t_0 = t_now;
}
}
}
// Alarm SETTING ***************************************************************************************//
if (Bt_Alarm_State == 0) {
Cursor2++;
delay(200);
AHH = HH;
AMM = MM;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Aarm Setting");
}
while (Cursor2 == 1 || Cursor2 == 2) {
ReadInput();
if (Bt_Alarm_State) {
Cursor2++;
delay(200);
if (Cursor2 > 3) Cursor2 = 0;
}
}
//***************************************************************************************************//
//***************************************************************************************************//
/*if (AHH == HH && AMM == MM) {
ReadInput();
if (Bt_SetTime_State == 0) {
AHH = -1;
}
tone(Buzzer, 2000);
delay(100);
noTone(Buzzer);
delay(100);
}
else {
noTone(Buzzer);
delay(10);
}*/
}
//********************************* Home Vew *****************************************//
void HomeView() {
//lcd.clear();
lcd.setCursor(1, 0);
lcd.print(dayOfWeek[Day]);
lcd.print(" ");
if (DD < 10) {
lcd.print("0");
}
lcd.print(DD);
lcd.print("/");
if (MMMM < 10) {
lcd.print("0");
}
lcd.print(MMMM);
lcd.print("/");
lcd.print(YYYY);
lcd.setCursor(4, 1);
if (HH < 10) {
lcd.print("0");
}
lcd.print(HH);
lcd.print(":");
if (MM < 10) {
lcd.print("0");
}
lcd.print(MM);
lcd.print(":");
if (ss < 10) {
lcd.print("0");
}
lcd.print(ss);
}
//********************************* Reading Input **************************************//
void ReadInput() {
Bt_Alarm_State = digitalRead(Bt_SetAlarm);
Bt_Down_State = digitalRead(Bt_Down);
Bt_Up_State = digitalRead(Bt_Up);
Bt_SetTime_State = digitalRead(Bt_SetTime);
}
//****************************************************************************************
void SetAlarm() {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm HH:MM");
lcd.setCursor(6, 1);
if (AHH < 10) lcd.print("0");
lcd.print(AHH);
lcd.setCursor(8, 1);
lcd.print(":");
lcd.setCursor(9, 1);
if (AMM < 10) lcd.print("0");
lcd.print(AMM);
ReadInput();
if (Bt_Down_State == 0) {
AHH++;
if (AHH > 23) AHH = 0;
delay(200);
}
if (Bt_Up_State == 0) {
AMM++;
if (AMM > 60) AMM = 0;
delay(150);
}
if (Bt_SetTime_State == 0) {
mode = 0;
lcd.clear();
lcd.print("Alarm Ring At ");
lcd.setCursor(9, 1);
if (AHH < 10) lcd.print("0");
lcd.print(AHH);
lcd.print(":");
if (AMM < 10) lcd.print("0");
lcd.print(AMM);
delay(500);
lcd.clear();
}
}
void SetTime() {
ReadInput();
if (Bt_Up_State == 0) {
UP();
delay(200);
}
if (Bt_Down_State == 0) {
DOWN();
}
}
// ***************************************************** //
// ***************** UP & DOWN FUNCTIONs *************** //
// ***************************************************** //
void UP() {
if (Cursor1 == 1) {
NewHH++;
if (NewHH > 23) {
NewHH = 0;
}
delay(200);
lcd.setCursor(4, 1);
if (NewHH < 10) lcd.print("0");
lcd.print(NewHH);
}
if (Cursor1 == 2) {
NewMM++;
if (NewMM > 59) {
NewMM = 0;
}
delay(200);
lcd.setCursor(7, 1);
if (NewMM < 10) lcd.print("0");
lcd.print(NewMM);
}
if (Cursor1 == 3) {
Newss++;
if (Newss > 59) {
Newss = 0;
}
delay(200);
lcd.setCursor(10, 1);
if (Newss < 10) lcd.print("0");
lcd.print(Newss);
}
if (Cursor1 == 4) {
NewDD++;
if (NewDD > 31) {
NewDD = 1;
}
delay(200);
lcd.setCursor(5, 0);
if (NewDD < 10) lcd.print("0");
lcd.print(NewDD);
}
if (Cursor1 == 5) {
NewMMMM++;
if (NewMMMM > 12) {
NewMMMM = 1;
}
delay(200);
lcd.setCursor(8, 0);
if (NewMMMM < 10) lcd.print("0");
lcd.print(NewMMMM);
}
if (Cursor1 == 6) {
NewYYYY++;
delay(200);
lcd.setCursor(11, 0);
lcd.print(NewYYYY);
}
// *********************************************** //
}
void DOWN() {
if (Cursor1 == 1) {
NewHH--;
if (NewHH < 0) {
NewHH = 23;
}
delay(200);
lcd.setCursor(4, 1);
if (NewHH < 10) lcd.print("0");
lcd.print(NewHH);
}
if (Cursor1 == 2) {
NewMM--;
if (NewMM < 0) {
NewMM = 59;
}
delay(200);
lcd.setCursor(7, 1);
if (NewMM < 10) lcd.print("0");
lcd.print(NewMM);
}
if (Cursor1 == 3) {
Newss--;
if (Newss < 0) {
Newss = 59;
}
delay(200);
lcd.setCursor(10, 1);
if (Newss < 10) lcd.print("0");
lcd.print(Newss);
}
if (Cursor1 == 4) {
NewDD--;
if (NewDD < 1) {
NewDD = 31;
}
delay(200);
lcd.setCursor(5, 0);
if (NewDD < 10) lcd.print("0");
lcd.print(NewDD);
}
if (Cursor1 == 5) {
NewMMMM--;
if (NewMMMM < 1) {
NewMMMM = 12;
}
delay(200);
lcd.setCursor(8, 0);
if (NewMMMM < 10) lcd.print("0");
lcd.print(NewMMMM);
}
if (Cursor1 == 6) {
NewYYYY--;
delay(200);
lcd.setCursor(11, 0);
lcd.print(NewYYYY);
}
// *********************************************** //
}
// ***************************************************** //
// UPDATE Time *************************************************************************//
void UdateTime() {
RtcDateTime currentTime = RtcDateTime(NewYYYY, NewMMMM, NewDD, NewHH, NewMM, Newss);
Rtc.SetDateTime(currentTime);
HH = NewHH;
MM = NewMM;
ss = Newss;
DD = NewDD;
MMMM = NewMMMM;
YYYY = NewYYYY;
}