Hello everyone
I wanted to combine the RTClib.h for it's ability to set time through the compiler and the TimeLib.h for its time_t variable and the ease of handling time and time calculations.
Here is my code relevant for the combination:
#include <Wire.h>
#include <RTClib.h>
#include <TimeLib.h>
RTC_DS3231 rtc; //Initialize RTC object
DateTime nowRTC = 0;
time_t nowTime = 0;
void setup()
{
Serial.begin(9600); // for debugging
#ifdef AVR
Wire.begin();
#endif
rtc.begin();
/*Sanity check*/
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time! ...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set the time after power has been lost
Serial.println("Time set!");
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // comment out after use and reupload
nowRTC = rtc.now();
nowTime = nowRTC.unixtime();
setSyncProvider(nowTime);
/*Sanity Check Time.h*/
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
nowRTC = rtc.now();
nowTime = nowRTC.unixtime();
setTime(nowTime);
/*For Debugging*/
Serial.print("The Time is: ");
Serial.print(hour());
Serial.print(" : ");
Serial.print(minute());
Serial.print(" : ");
Serial.println(second());
Serial.print("The Date is: ");
Serial.print(day());
Serial.print(".");
Serial.print(month());
Serial.print(".");
Serial.println(year());
}
Now what happens is that the Arduino gives me the message: "Unable to sync with the RTC". The time seems to work ok. But the date is completly off. Instead of 28.07.2019 it gave me 65.03.2019.
What might be wrong in my code?
Could anyone tell me at the same time how to use the macros of TimeLib.h? For instance:
#define elapsedSecsToday(time) ((time) % SECS_PER_DAY)
Thank you all!
moses