I'm using the TimeLib.h library to keep track of time. This is working fine. When I try to tell it to get the time from the DS3231 using the setSyncProvider , it outputs absolutely nothing.
It is pulling the time from the setSyncProvider - I know this because if I set the value of timeUL in time_provider() to a value e.g. 2000000, it prints a time to the serial monitor every second, and starts again from the same time every 5 seconds.
I also know that rtc.getUnixTime(rtc.getTime()); does return the epoch time as it works when I try calling it from setup().
Can anyone tell me what I may be doing wrong?
#include <TimeLib.h>
#include <DS3231.h> // For real time clock
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
void setup() {
setSyncProvider(time_provider);
setSyncInterval(5);
Serial.begin(9600);
Serial.println();
// Initialize the rtc object
rtc.begin();
// Test
Serial.println(rtc.getUnixTime(rtc.getTime()));
Serial.println();
}
void loop() {
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
time_t time_provider() {
time_t timeUL;
timeUL = 200000000; //rtc.getUnixTime(rtc.getTime());
return timeUL;
}