#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 mode = 0;
int prevCount = 0;
int flag = 1;
ezButton button(sw);
void setup() {
Serial.begin(9600);
button.setDebounceTime(25);
if (!rtc.begin()) {
Serial.println("RTC not initialised");
while (true);
}
attachInterrupt(digitalPinToInterrupt(clk), encoder, FALLING);
}
void encoder() {
prevCount = mode;
if (digitalRead(dt) == HIGH) mode++;
else mode--;
mode = constrain(mode, 0, 3);
flag = 1;
}
void changeMode() {
if (flag == 1 && prevCount != mode) {
if (mode == 0) {
Serial.println("Current Time");
}
else if (mode == 1) {
Serial.println("Alarm");
}
else if (mode == 2) {
Serial.println("Stopwatch");
}
else if (mode == 3) {
Serial.println("World Clock");
}
flag = 0;
}
}
void modeSelect() {
if (mode == 0) {
Serial.println("Date and time mode selected");
}
if (mode == 1) {
Serial.println("Alarm mode selected");
}
if (mode == 2) {
Serial.println("Stopwatch mode selected");
}
if (mode == 3) {
Serial.println("World clock mode selected");
}
}
void loop() {
button.loop();
if (button.isPressed()) {
modeSelect();
}
changeMode();
delay(10);
}
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;
}