#include<RTClib.h>
#include<ezButton.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4);
// encoder variables
byte clk = 2;
byte dt = 3;
byte sw = 4;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
ezButton button(sw);
// counting rotations
int counter = 0;
int prev_counter = -1;
int flag = 1;
int stopwatchH = 0;
int stopwatchM = 0;
int stopwatchS = 0;
String rtc_date = "";
String rtc_time = "";
void setup() {
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
lcdPrint(0, 0, "RTC FOUND");
delay(1000);
lcdPrint(0, 0, "Hi Im a clock");
delay(1000);
// if (!rtc.begin()) {
// Serial.println("RTC not initialised");
// while (true);
// }
// Serial.println("RTC found");
attachInterrupt(digitalPinToInterrupt(clk), encoder, FALLING);
button.setDebounceTime(25);
}
void encoder() {
prev_counter = counter;
if (digitalRead(dt) == HIGH)counter++;
else counter--;
counter = constrain(counter, 0, 3);
flag = 1;
}
void mode_selector() {
if (prev_counter != counter && flag == 1) {
if (counter == 0) {
lcd.clear();
lcdPrint(0, 0, "Date and Time");
}
else if (counter == 1) {
lcd.clear();
lcdPrint(0, 0, "Set Alarm");
}
else if (counter == 2) {
lcd.clear();
lcdPrint(0, 0, "Stopwatch");
}
else if (counter == 3) {
lcd.clear();
lcdPrint(0, 0, "Countdown Timer");
}
flag = 0;
}
}
void loop() {
button.loop();
if (button.isPressed())select_mode();
// 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();
// for better working of simulator
delay(10);
}
String get_time() {
while (true){
button.loop();
if(button.isPressed()){
lcd.clear();
prev_counter = -1;
flag = 1;
break;
}
currentTime();
String current_time = "Time : " + String(hour) + ":" + String(minute) +
":" + String(second);
String current_date = "Date : " + String(day) + "/" + String(month) +
"/" + String(year);
lcdPrint(0,0,"Date & Time");
lcdPrint(0,1,current_date);
lcdPrint(0,2,current_time);
}
}
String get_date(DateTime current) {
int year = current.year();
int month = current.month();
int day = current.day();
}
void select_mode() {
if (counter == 0)get_time();
else if (counter == 1)setAlarm();
else if (counter == 2)stopwatch();
else { countdown();}
}
void lcdPrint(int x, int y, String message) {
lcd.setCursor(x, y);
lcd.print(message);
}
void setAlarm(){
}
void stopwatch(){
int lastSecond = 0;
while (true){
button.loop();
if (button.isPressed()){
prev_counter = -1
flag = 1;
delay(5000);
lcd.clear();
break;
}
currentTime();
if (abs(second-lastSecond)>=1){
lastSecond = second;
String stopwatchTime = String(stopwatchH) + ":" + String(stopwatchM) +
":" + String(stopwatchS);
lcd.clear();
lcdPrint(0,0,"StopWatch");
lcdPrint(0,1,String (stopwatchTime));
stopwatchS ++;
}
if (stopwatchS > 59){
stopwatchM ++;
stopwatchS = 0;
}
else if (stopwatchM > 59){
stopwatchH ++;
stopwatchM = 0;
}
else if (stopwatchH > 23){
stopwatchH = 0;
stopwatchM = 0;
stopwatchS = 0;
}
}
}
void countdown(){
}
void currentTime(){
DateTime current = rtc.now();
year = current.year();
month = current.month();
day = current.day();
hour = cuurent.hour();
minute = current.minute();
second = current.second();
}