#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;
byte buzzer_pin = 5;
ezButton button(sw);
// counting rotations
int counter = 0;
int prev_counter = -1;
int flag = 1;
// Date and time
int year = 0, month = 0, day = 0;
int hour = 0, minute = 0, second = 0;
int alarm_hours = 0, alarm_minutes = 0;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
if (!rtc.begin()) {
Serial.println("RTC not initialised");
while (true);
}
lcd_print(0, 0, "RTC found");
delay(500);
attachInterrupt(digitalPinToInterrupt(clk), encoder, FALLING);
button.setDebounceTime(25);
}
void encoder() {
prev_counter = counter;
if (digitalRead(dt) == HIGH) counter++;
else counter--;
flag = 1;
}
void mode_selector() {
counter = constrain(counter, 0, 3);
if (prev_counter != counter && flag == 1) {
if (counter == 0) {
lcd.clear();
lcd_print(0, 1, "Date and Time?");
}
else if (counter == 1) {
lcd.clear();
lcd_print(0, 1, "Set Alarm?");
}
else if (counter == 2) {
lcd.clear();
lcd_print(0, 1, "Stopwatch?");
}
else if (counter == 3) {
lcd.clear();
lcd_print(0, 1, "Countdown Timer?");
}
flag = 0;
}
}
void loop() {
button.loop();
if (button.isPressed()) select_mode();
mode_selector();
currentTime();
if (alarm_minutes == minute && alarm_hours == hour) {
playBuzzer(1000, 500);
alarm_hours, alarm_minutes = 0;
}
delay(50);
}
void select_mode(){
if (counter == 0) show_time();
else if (counter == 1) set_alarm();
else if (counter == 2) stopwatch();
else countdown();
}
void currentTime() {
DateTime current = rtc.now();
year = current.year();
month = current.month();
day = current.day();
hour = current.hour();
minute = current.minute();
second = current.second();
}
void show_time() {
lcd.clear();
lcd_print(1, 0, "Date and Time:");
lcd_print(0, 1, "Date:-");
lcd_print(0, 2, "Time:-");
while (true) {
button.loop();
if (button.isPressed()) {
lcd.clear();
prev_counter = -1;
flag = 1;
break;
}
currentTime();
String currentDate = String(day) + "/" + String(month) + "/" + String(year);
String currentTime = String(hour) + ":" + String(minute) + ":" + String(second);
lcd_print(7, 1, " ");
lcd_print(7, 2, " ");
lcd_print(7, 1, currentDate);
lcd_print(7, 2, currentTime);
delay(50);
}
}
void set_alarm() {
lcd.clear();
lcd_print(0, 0, "Alarm");
lcd_print(0, 1, "Enter hours: ");
alarm_hours = set_value(0, 23);
lcd.clear();
lcd_print(0, 0, "Alarm");
lcd_print(0, 1, "Enter minutes: ");
alarm_minutes = set_value(0, 59);
lcd.clear();
lcd_print(0, 1, "Alarm set successfully");
delay(500);
}
void stopwatch(){
int stopwatch_hour, stopwatch_minute, stopwatch_second = 0;
while (true) {
button.loop();
if (button.isPressed()) {
lcd.clear();
prev_counter = -1;
flag = 1;
break;
}
String stopwatch_time = String(stopwatch_hour) + ":" + String(stopwatch_minute) + ":" + String(stopwatch_second);
lcd.clear();
lcd_print(0, 0, "Stopwatch:");
lcd_print(0, 1, stopwatch_time);
stopwatch_second++;
if (stopwatch_second > 59) {
stopwatch_second = 0;
stopwatch_minute++;
} else if (stopwatch_minute > 59) {
stopwatch_minute = 0;
stopwatch_hour++;
} else if (stopwatch_hour > 23) {
stopwatch_hour, stopwatch_minute, stopwatch_second = 0;
}
}
}
void countdown() {
int cd_mins, cd_secs = 0;
lcd.clear();
lcd_print(0, 0, "Countdown Timer");
lcd_print(0, 1, "Enter minutes: ");
cd_mins = set_value(0, 59);
lcd.clear();
lcd_print(0, 0, "Countdown Timer");
lcd_print(0, 1, "Enter seconds: ");
cd_secs = set_value(0, 59);
while (cd_mins != 0 || cd_secs != 0) {
lcd.clear();
lcd_print(0, 1, "Countdown Timer:");
String msg = String(cd_mins) + ":" + String(cd_secs);
lcd_print(0, 2, msg);
if (cd_secs == 0) {
if (cd_mins !=0 ) {
cd_secs = 59;
cd_mins--;
}
}
cd_secs--;
delay(1000);
}
lcd.clear();
lcd_print(0, 0, "Countdown Over");
playBuzzer(2000, 3000);
}
int set_value(int minVal, int maxVal) {
int value = 0;
counter = 0;
while(true) {
button.loop();
if (button.isPressed()) break;
counter = constrain(counter, minVal, maxVal);
lcd_print(0, 2, " ");
lcd_print(0, 2, String(counter));
}
value = counter;
return value;
}
void playBuzzer(int freq, int dur) {
tone(buzzer_pin, freq);
delay(dur);
noTone(buzzer_pin);
}
void lcd_print(int x, int y, String message) {
lcd.setCursor(x, y);
lcd.print(message);
}