0

I have an Arduino based countdown timer that counts down the number of minutes remaining to an event and displays them on an LCD. Since the event's start time is fixed, I have to re-calculate my countdown duration and update a constant in the sketch every time I upload it.

Is it possible to calculate the duration on the PC and set the starting value for the timer automatically? Perhaps using some kind of pre-compile script? Or perhaps through some kind of debugging features?

I'm not interested in adding RTC or other hardware to this project b/c (1) I don't have it handy and (2) there's very limited physical space in the already assembled project.

I'm currently using the Arduino IDE but I would be open to solutions that use other IDEs, or external components.

9
  • Send it through serial? – gre_gor May 31 '17 at 18:47
  • Using a RTC would be better in this case, as it always keeps time, even when the Arduino isn't powered. – Gerben May 31 '17 at 18:53
  • i would just use an ESP8266 to fetch the actual time from the web onboot. if you use epocs, you can just subtract the fetched value from the stored value to get "how much longer". this works across reboots and is immune to internal counter rollover. – dandavis May 31 '17 at 19:00
  • I am avoiding changing the hardware at this point b/c (1) I don't have it handy and (2) there's very limited physical space in the already assembled project. However the ideas are valid; for another planned project I will be adding both Wi-Fi and an RTC for exactly this purpose. – Jens Ehrich May 31 '17 at 19:11
  • @JensEhrich: if you have wifi+web, you don't need an RTC, just load time on each boot, takes 2 seconds... – dandavis May 31 '17 at 19:46
3

The C compilers provide two macros that resolve to string literals: __DATE__ gives the time the file was compiled, and __TIME__ gives the current time of day. They output e.g. "May 31 2017" and "21:19:49"

You'd have to parse these string to get the time and do the calculations to get the remaining time.

5
  • PC's date is not likely to be set for the future, can you elaborate on how this plan operates? – dandavis May 31 '17 at 18:48
  • You know the date and time of the event. You know the current time, through the __DATE__ and __TIME__ constants. Subtract the latter from the former, and you get the number of minutes remaining. (provided the device doesn't lose power, and uses the now wrong value of __DATE__ and __TIME__ – Gerben May 31 '17 at 18:52
  • This is pretty much exactly what I was after. It will certainly solve the problem for this scenario; I'm aware of the clock reset issue, and have a battery backup on the timer already. – Jens Ehrich May 31 '17 at 18:59
  • I'd suggest using a time library to parse the string and to do the date difference math. – Gerben May 31 '17 at 19:42
  • 1
    IIRC TimeLib.h can work directly with these macros. – Majenko May 31 '17 at 21:12
0

Store your time acquired the way Gerben is describing.

If you have a fixed future date, add that date with the compilation date (and calculate the number of seconds/hours).

Note however, this only works when you don't reset your Arduino, or might expect a power loss in the future. If this can happens, store the current time + event time in EEPROM.

Also, add some seconds in case accuracy is important between the compilation time and time for uploading/resetting the Arduino.

Also take into account that the normal way to get time is millis(), and this might cause overflows when used in calculations. So use a second counter to convert/check for overflows and convert to minutes/hours for example.e

2
  • 1
    Also, millis will start back from 0 after around 50 days. So you have to work around that if you want your countdown to be more that 50 days into the future. – Gerben May 31 '17 at 18:56
  • 1
    The tip for adjusting the seconds between compile and run is a good one, but I don't need that kind of accuracy here. The target duration is within the limits of millis() but I'm still tracking full hours with a separate counter anyway. Thanks Michel. – Jens Ehrich May 31 '17 at 19:04

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.