#include <RTClib.h>
#include <ezButton.h>
RTC_DS1307 rtc;
String rtc_date = "";
String rtc_time = "";
byte clk = 2;
byte dt = 3;
byte sw = 4;
int counter = 0;
int prev_counter = 0;
int flag = 0;
ezButton button(sw);
void setup(){
Serial.begin(9600);
button.setDebounceTime(25);
attachInterrupt(digitalPinToInterrupt(clk) , encoder , FALLING);
// (interupt, function, mode)
if (!rtc.begin()){
Serial.println("RTC not initialised");
while(true);
}
Serial.println("RTC found");
}
void loop(){
// RTC date and time
DateTime dt = rtc.now();
rtc_date = get_date(dt);
Serial.println(rtc_date);
rtc_time = get_time(dt);
Serial.println(rtc_time);
mode_selector();
button.loop();
if (button.isPressed())select_mode();
// for better working of simulator
delay(1000);
}
String get_time(DateTime current){
int hour = current.hour();
int minute = current.minute();
int second = current.second();
String current_time = "Time : " + String(hour) + ":" + String(minute) +
":" + String(second);
return current_time;
}
String get_date(DateTime current){
int year = current.year();
int month = current.month();
int day = current.day();
String current_date = "Date : " + String(day) + "/" + String(month) +
"/" + String(year);
return current_date;
}
void encoder(){
if (digitalRead(dt) == HIGH)counter++;
else counter--;
counter = constrain(counter , 0 , 3);
//(,lower,upper)
flag = 1;
}
void mode_selector(){
if (counter == 0){
Serial.println("Current Time");
}
else if (counter == 1){
Serial.println("Set Alarm");
}
else if (counter == 2){
Serial.println("Stopwatch");
}
else if (counter == 3){
Serial.println("World clock");
}
flag = 0;
}
void select_mode(){
if(counter == 0)Serial.println("Date and Time mode is selected");
if(counter == 1)Serial.println("Set Alarm mode is selected");
if(counter == 2)Serial.println("Stopwatch mode is selected");
if(counter == 3)Serial.println("Countdown Timer mode is selected");
}