#include <RTClib.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"SUN", "MON", "TUe", "WED", "THU", "FRI", "SAT"
};
int oldSecond; // print new data every change in "second"
void setup () {
Serial.begin(115200);
rtc.begin();
// if (! rtc.begin()) { // uncomment this block if you can not get an RTC reading
// Serial.println("Couldn't find RTC");
// Serial.flush();
// while (1);
// }
// automatically set the RTC to the date & time on PC this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// manually set RTC with an explicit date & time...
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
if (oldSecond != now.second()) { // only print when seconds change
zeropad(now.year(), "/"); // format the value and add a delimeter
zeropad(now.month(), "/");
zeropad(now.day(), " (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
zeropad(now.hour(), ":");
zeropad(now.minute(), ":");
zeropad(now.second(), " ");
oldSecond = now.second(); // store old "seconds"
Serial.println();
}
}
void zeropad(int value, char symbol[]) {
if (value < 10)
Serial.print("0"); // pad the units value
Serial.print(value); // print the value (year, month, day, hour, minute, second)
Serial.print(symbol); // print a delimiter symbol
}