Using the ds1307 sketch from the driver example folder.
#include "SdFat.h"
#include "RTClib.h"
I have a sketch that checks the clock and reports the time and an analog value to the Serial port, and it tells the correct time. When I use the same statements to report to the SD logfile the time is wrong: 2/6/2106 6:28:16. I then decided to learn what I could playing with the DS1307 sketch. Modified the ds1307 sketch to wait a second, print the seconds again, wait 2+ more seconds and print the seconds value and they all 3 print the same value for seconds. I then tried various ways to "reread" the clock values,but any call I make crashes the compiler. I have tried restating the definition DateTime now = rtc.now(); but it doesn't like that because I am declaring the same thing twice. Using now = rtc.now(); crashes in compile.
So what actually assigns the time value to the variable and how to reread it? I do not see in the example what reassigns the value to the variable - where does the call to read the clock happen?
In my program, when I write the data to the SD card it write the reading OK but reports the time as the 2106 year, always the same. So I tried to reread the seconds value and it does not change within the same loop iteration. Next loop it is updated (~3 seconds later).
Sample output:
1/12/2017 12:3:40 40 40
You see that it prints the seconds value 3 times with delay, but it is always the same value for the same pass through the loop.
1484222620
1/12/2017 12:3:43 43 43
1484222623
Here is the code. I decided to test using the ds1307.ino sketch as here and make it delay and print the seconds 3 times: Serial.print(now.second(), DEC);
This is related to my actual problem where I print the time to the serial port and it is OK, then when I print it to the SD Card file it reports year 2106. The clock is running and works every time I load it.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib.
// Prints the secondd field out 3 times. Does it update?
// Mod 1/12/2017 Bill Allen
//
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(115200);
// if (! rtc.begin()) {
rtc.begin();
// if (! rtc.isrunning()) {
// Serial.println("Couldn't find RTC");
// while (1);
// }
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(DATE), F(TIME)));
// 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));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(1200);
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(2200);
Serial.println(now.second(), DEC); // The consecutive 'seconds' all print the same value!
// SO, what command / statement actually does the update?
Serial.println(now.unixtime());
}