# Calculate the time since arduino started working for a year

I want Calculate the time since arduino started working for a period of one year using arduino uno and when the period is over flip a light on

• I'd use an RTC, as you only need one power outage for the system to fail (without an RTC). – Gerben Mar 3 '17 at 8:34

Simplest solution would be to use `millis()`, but you will need to somehow take care of the periodic rollover. Creating a lower frequency software timer, as explained by Jot, is one solution. Another option is to extend `millis()` to 64 bits:

``````uint64_t millis64()
{
static uint32_t low32, high32;
uint32_t new_low32 = millis();
if (new_low32 < low32) high32++;
low32 = new_low32;
return (uint64_t) high32 << 32 | low32;
}
``````

You must call this function at least once every 49.7 days, otherwise it will loose time. I would just call it on every loop iteration, like this:

``````const uint64_t ONE_YEAR = 365.25 * 86.4e6;  // Julian year

void loop()
{
if (millis64() >= ONE_YEAR) {
// do some stuff...
}
}
``````

Besides the issue of a possible power failure, there is another caveat: the Arduino Uno is clocked off a ceramic resonator. These are not very accurate, and are typically specified to be within 0.5% of their nominal frequency. This means that your “year” can be off by as much as 1.8 days. RTCs are clocked off a quartz crystal and are then far more accurate. Ultimate accuracy could be achieved with a GPS receiver.

The function millis counts up to about 50 days. However, you can make a software timer with millis of, for example, 1Hz. With a unsigned long that counts the seconds, it is easy to count a year.

It will not be reliable. When the power is off, it starts again. A RTC (Real Time Clock) with its own battery is better.

Are you a good software engineer ? How can you be sure that it will work ?

I would use a network card to talk to an NTP server. In setup(), fetch the time and save it to EEPROM if there's not one already saved. You can then ping the NTP server every day or every boot, comparing the saved date and responding as appropriate. This strategy avoids MCU timer slew, power-out milis() resets, and extra hardware (other than networking, which more and more projects have already anyway).