Hello.
I'm currently doing a project (a clock) with an Arduino and a RTC DS1307 (mainly), but I have some problems putting everything together.
I'm using MsTimer2 to create interruptions every 5 seconds, to request the time from the RTC.
But the function rtc.now() seems to lock the interruption. It works fine if I use rtc.now() in the main loop with a delay, but it's active delay (which I try to avoid).
Here is my code :
#include <Wire.h>
#include <RTClib.h>
#include <MsTimer2.h>
RTC_DS1307 rtc;
void setup() {
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
now = rtc.now();
init_interrupt();
}
void loop() {
// This works, but with active delay
/*DateTime now = rtc.now();
Serial.print(now.second(), DEC);
Serial.println();
delay(5000);*/
}
void init_interrupt(){
MsTimer2::set(1000, callback); // 5s period
MsTimer2::start();
}
void callback(){
// Here it doesn't work
now = rtc.now();
Serial.print("callback ");
Serial.print(now.second());
Serial.println();
}
I don't know what I'm missing. Is it possible that rtc.now() must be synchroneous ? (therefore not working in a interruption). What can I do to make it work ?
Thank you for your help.
Lothigo.