2

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
    Remove that line of code. You only need to set the time once. – Gerben Dec 1 '15 at 15:05
  • But how do I set the time for the first time? Upload the program with this line and then upload it for the second time without this line? – Rok Dolinar Dec 1 '15 at 15:13
  • 4
    Make a time-setting function in your code, and in your 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. – dda Dec 1 '15 at 17:23
  • 1
    How are you accepting commands for your application? Basically whatever that process is, add another command to set the time. – dlu Dec 1 '15 at 20:23
  • 1
    Oh, you'd set it to whatever time the user (the issuer of the command) says it is. In the example above the time is 2015-12-01 10:10:10 (today at 10:10:10 in the morning, presumably local time). The string of numbers would need to be parsed into something that could be used to set the RTC. – dlu Dec 1 '15 at 21:09
4

Without knowing the DS1307 well, I think there are two options:

  1. 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

  2. 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.

| improve this answer | |
  • 1
    No. You will see that you will need to readjust the time regularly. The DS1307 isn't that precise. – dda Dec 1 '15 at 17:20
  • 1
    But 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 Dec 1 '15 at 17:59
  • 2
    No 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 Dec 1 '15 at 18:02
  • 1
    NTP 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 Dec 1 '15 at 18:58
  • 1
    I 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 Dec 1 '15 at 20:50
1

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

  1. Deal with restart issues in setup() as it will only be called once per session (unless you call it in your own code); or

  2. 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, including PowerCycled with it's initial value of true restored.

| improve this answer | |
  • Yeah, I'll probably go with boolean variable and it should work just fine, thank you! – Rok Dolinar Dec 5 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.