//RtcDS1307_Test01a
//https://wolles-elektronikkiste.de/ds1302-und-ds1307-rtc-real-time-clock
String version = "RtcDS1307_Test01a v0.00";
#include <Wire.h>
//#include <DS1307RTC.h>
#include "RtcDS1307.h"
//#include <RtcDS1307.h>
RtcDS1307<TwoWire> rtc(Wire);
void setup () {
Serial.begin(9600);
Serial.println(version);
rtc.Begin();
if(!rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
rtc.SetIsRunning(true);
}
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
Serial.println("Compile Time:");
printDateTime(compiled);
Serial.println();
rtc.SetDateTime(compiled);
Serial.println("Enter the new time as hh:mm:ss");
}
void loop () {
RtcDateTime now = rtc.GetDateTime();
static int currentSecond = 61; // currentSecond to be different from first nextSecond
int nextSecond = now.Second();
if(nextSecond != currentSecond){
printDateTime(now);
Serial.println();
currentSecond = nextSecond;
}
if(Serial.available()){
uint8_t newHour = Serial.parseInt();
uint8_t newMinute = Serial.parseInt();
uint8_t newSecond = Serial.parseInt();
rtc.SetDateTime(RtcDateTime(now.Year(),now.Month(),now.Day(),newHour, newMinute, newSecond));
while(Serial.available()){
Serial.read();
}
}
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt){
char datestring[25];
char daysOfTheWeek[7][4] = {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
snprintf_P(datestring,
countof(datestring),
PSTR("%3s, %02u.%02u.%04u %02u:%02u:%02u"),
daysOfTheWeek[dt.DayOfWeek()],
dt.Day(),
dt.Month(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}