void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
// Check to see if the RTC is keeping time. If it is, load the time from your computer.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// This will reflect the time that your sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
I don't quite understand why the if statement also includes what to do when the criteria are not met. I would write the script like this:
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
// Check to see if the RTC is keeping time. If it is, load the time from your computer.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
}
else {
// This will reflect the time that your sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
Both versions seem to work but I don't see how the first one does. Can someone explain it to me?
What I fail to understand is why in the first script the RTC.adjust(DateTime(__DATE__, __TIME__)); appears within the if (! RTC.isrunning()) {} statement. If the RTC is not running then surely there is no need to do anything except warn the user that the RTC is not running.
There is only a need to adjust the DateTime if the RTC IS running.
RTC's have an enable bit that needs to be set for the clock to run. If its running its assumed
the time is correct, else its assumed this is the setup phase, so the time needs to be set and
the RTC set running.