I have a new DS1307 (original, not a Chinese rip off), with a new battery. It is working great, but when I turn the Arduino off (it is currently connected to laptop) and then turn it back on, it resets the time.
The line that sets the time is (in setup part of code):
setDS1307time(30, 42, 21, 4, 26, 11, 14);
So, when the Arduino gets the power back it runs the program again and it overrides the correct current time.
How can I avoid this?
2 Answers 2
Without knowing the DS1307 well, I think there are two options:
Make the setting of the clock a UI function (you should probably do this in any case since you will want users to have the ability to set the clock). The user would have to initiate the action (and put in the time to set) so there would be no problems the clock getting reset inappropriately, or
Do a check for sanity of the clock before you run that line. You might be able to do that through the clock API (e.g., make a call to check the time, or to get the clock status and see if it makes sense) or you could do it by storing the clock status in nonvolatile memory. The very worst case would be that you store the time of last boot and check that it seems reasonable (e.g., the time you get from the RTC is after the last boot and is within reason – say no more than a month or a year ago). If the time is too far off to feel confident you could issue a "check time" warning.
If you have a network connection using NTP as well as the RTC would be a very smart option. NTP can hold the time within a few milliseconds. The RTC would then provide you with protection against a network outage.
If you are using the Arduino to manage scheduled events one thing you'll want to think about is events that are scheduled in a moment of time that is either "skipped" as you advance the clock or that "repeats" if you set the clock back. One way to handle that is to never "jump" time, but to always just speed it up or slow it down.
-
1No. You will see that you will need to readjust the time regularly. The DS1307 isn't that precise.dda– dda2015年12月01日 17:20:58 +00:00Commented Dec 1, 2015 at 17:20
-
1But since you have a network connection, it would be good to look into running NTP. NTP (network time protocol) will hold your time to within a fraction of a second.dlu– dlu2015年12月01日 17:59:56 +00:00Commented Dec 1, 2015 at 17:59
-
2No matter how good the RTC is there are reasons why you might want to set the time – battery failure, summer time, moving to a different time zone, initial setting error, ...).dlu– dlu2015年12月01日 18:02:10 +00:00Commented Dec 1, 2015 at 18:02
-
1NTP expects the network to go down from time to time. It will recover as soon as it can communicate with its peers again. In the mean time the RTC will help you stay close and will give you a good estimate of the time if you need to reboot. You could use NTP to update the RTC from time to time as well.dlu– dlu2015年12月01日 18:58:23 +00:00Commented Dec 1, 2015 at 18:58
-
1I would just let it run. You could cut it down to every few minutes or even every day depending on how good a job the Arduino is doing of keeping time. But I don't think you'll have issues of CPU utilization.dlu– dlu2015年12月01日 20:50:02 +00:00Commented Dec 1, 2015 at 20:50
If setup() runs, it is because the system has been restarted. It could becuase the reset button was pushed, or it could be due to a power-cycle. But either way, the code will start over completely. So you could
Deal with restart issues in setup() as it will only be called once per session (unless you call it in your own code); or
Make a variable
static boolean PowerCycled = true;
and clear it once your code has dealt with its re-start issues. After a restart, the initialzed data will get reloaded, includingPowerCycled
with it's initial value oftrue
restored.
-
Yeah, I'll probably go with boolean variable and it should work just fine, thank you!Rok Dolinar– Rok Dolinar2015年12月05日 15:22:57 +00:00Commented Dec 5, 2015 at 15:22
loop()
code watch for a specific command that sets the time. I use 'T20151201101010' in my own machines. When they get such a command, they adjust their clock. You will see that the DS1307 can grow slowly off-base, and having a time-setting function comes handy.