#include <RTClib.h>
#include <ezButton.h>
byte CLK = 2;
byte DT = 3;
byte SW = 4;
int prev_count = 0;
int counter = 0;
int flag = 0;
ezButton button(SW);
RTC_DS1307 rtc;
String rtc_date = "";
String rtc_time = "";
void setup() {
Serial.begin(9600);
if (!rtc.begin()){
//Serial.println("RTC has not been initialised");
while(1);
}
//Serial.println("RTC found");
attachInterrupt(digitalPinToInterrupt(CLK), encoder, FALLING);
button.setDebounceTime(25);
}
void encoder(){
prev_count = counter;
if (digitalRead(DT) == HIGH) counter++;
else counter--;
counter = constrain(counter, 0, 3);
flag = 1;
}
void mode_selector(){
if (flag == 1 && prev_count != counter){
if (counter == 0){
Serial.println("Date and Time");
}
else if (counter == 1){
Serial.println("Set Alarm");
}
else if (counter == 2){
Serial.println("Stopwatch");
}
else if (counter == 3){
Serial.println("Timer");
}
flag = 0;
}
}
void loop() {
if (button.isPressed()) select_mode();
//DateTime dt = rtc.now();
//rtc_date = get_date(dt);
//rtc_time = get_time(dt);
//Serial.println(rtc_date);
//Serial.println(rtc_time);
mode_selector();
delay(100);
}
void select_mode(){
if (counter == 0) Serial.println("Date and Time");
if (counter == 1) Serial.println("Set Alarm");
if (counter == 2) Serial.println("Stopwatch");
if (counter == 3) Serial.println("Timer");
}
String get_date(DateTime dt){
int day = dt.day();
int month = dt.month();
int year = dt.year();
String current_date = "Date : " + String(day) + "/" + String(month) + "/" + String(year);
return current_date;
}
String get_time(DateTime dt){
int hour = dt.hour();
int minute = dt.minute();
int second = dt.second();
String current_time = "Time : " + String(hour) + ":" + String(minute) + ":" + String(second);
return current_time;
}