mea culpe, I made a typographical error. The chip is (as noted in Post #1), a DS1307. I do not think that I ran the sketch, DS3231.ino by mistake, but ⸺ out of curiosity ⸺ I ran both versions of the Example sketches, and they both compiled and worked [exhibiting the same sort of behaviour that I reported initially]
Thanks to cedarlakeinstruments for the comment that the internal clock (i.e., of the Dué SAM microcontroller) was almost certainly not conflicting with the RTC mounted on the external shield.
Now...
It seems that the peculiar Lost Time behaviour I noted is rooted in my confused notion about how uploaded Arduino code behaves, generally, when power to the Arduino microcontroller is interrupted. Specifically, I mean that:
- I mistakenly believed that, whenever Arduino power is interrupted and for whatever reason, the code would only execute the previously active
loop routine. But it does not do that: it resumes by executing the entire sketch "from the top, down" [d'Oh. You learn something new every day, even when you are not trying to...].
The appearance of "lost time" resulted because:
- Not only did I not appreciate the fact that the Arduino would re-execute the entire sketch from-the-top-down; but, because of my ignorance,
- I also blissfully ignored the lines of code written above
loop, with the result that I did not comprehend how the if statement (see lines of code immediately below...) would behave, were that section of the code to be re-executed ; and so I now know that,
- To avoid the appearance of "Lost Time" it was necessary to comment-out some lines of code to constrain the behaviour of the Arduino microcontroller (not the RTC itself) after the microcontroller was disconnected from power, even if the RTC remained under power via the coin-cell battery. Once the sketch has been modified, that code must of course be uploaded to the microcontroller to replace the code that set the RTC in the first place!
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// 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));
}
// When time needs to be re-set on a previously configured device, the
// 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));
}
In short, as I now understand the system:
- First, you must know whether your RTC is actually keeping time, right now: If it is an RTC-on-a-shield, and iff a time-stamp was previously pushed to the RTC chip, and iff it has a battery installed as backup and iff the battery is still good, you have "a previously configured device" as referred to in Line 31 of the sketch
DS1307.ino
- If the RTC is not "previously configured" as defined above, the RTC has defaulted to it's factory time-stamp. In the case of the chips I have, the date & time is 2000/01/01 00:00:00
- If you have not sent a command of the form,
rtc.adjust(DateTime......))), the clock will not start running. In my case, the RTC constantly reports the static time-stamp ''2000/01/01 00:00:00'' for each and every instance that the loop is executed;
- iff the line of the code uploaded to and stored within the microcontroller
rtc.adjust(dateTime(F(__DATE__), F(__TIME__))) is active, and iff you turn-off the power to the microcontroller and then restore that power....the sketch re-executes from-the-top-down such that the RTC is re-set to the time "stored" in the sketch (i.e., the static, numerical value of the time that the sketch was compiled; which was subsequently uploaded & stored in the microcontroller-chip's memory)
Summary
- Once you have configured a lifeless RTC; or
- Once you have re-set the time on an RTC that was "previously configured" and up-and-running; and,
- iff your RTC has a backup coin-cell battery (i.e., making it independent of the Arduino microcontroller's power state); then
- You must comment-out all the
rtc.adjust(dateTime(.........) lines of code, followed by the recompile-and-upload the Example sketch, DS1307.ino (or its equivalent).
- Only under these conditions will the RTC keep time independently ⸺ regardless of how many times the Arduino microcontroller is switched on-and-off, and regardless of how long the microcontroller remains powered-down
{This has been a lengthy reply, to be sure, but it was written in the hopes that it will wrap-up the discussion and perhaps offer some insight. Comments are very welcome, of course}