Very long-term countdown timer (no display needed)

Hello

I'm interested in making a simple device that will count down around 360 days from the moment it is powered on and then flash an LED or some other simple alert at the end of the countdown. It needs to be accurate only to within a few days of the target, so I don't think elaborate clocks would be necessary.
I'm not sure where to start. I've done a fair amount of research on timers but all of the code I find is extremely complicated because the builds are far more fully-featured than I need. I basically need something like a "delay(1 year)" or a "if (millis > 1 year)" but I doubt those methods would be good (for one thing, I realize millis overflows too quickly).
Again, no read-out or display is necessary - it's a "set-it-and-forget-it" device that one will only notice around a year later.

I imagine there is a pretty easy solution that is obvious to someone who has more programming experience than I do. Any help is greatly appreciated.

Thanks!

There are a handful of ways to accomplish this. One way would be to take use the Blink without Delay example as the structure. Set the interval to 1 second, Every time 1 second occurs, add it to a seconds counter (use an unsigned long as the counter). Once your seconds are at 365 * 24 * 60 * 60, you've reached a year.

A simple solution. You need to test for off-by-one errors.
How are you going to test this?
How are you going to supply the power for a year?
What happens when the power goes off?

int seconds = 60;
int minutes = 60;
int hours = 24;
int days = 360;
boolean start_flash = false;

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT); 
  digitalWrite(13, LOW);  
}

void loop() {
  delay(1000);
  seconds -= 1;
  if (seconds <= 0)
  {
    seconds = 60;
    minutes -= 1;
  }
  if (minutes <= 0)
  {
    minutes = 60;
    hours -= 1;
  }
  if (hours <= 0)
  {
    hours = 24;
    days -= 1;
  }
  if (days == 0)
  {
    start_flash = true;
  }
  if (start_flash == true)
  {
     digitalWrite(13, HIGH);   // set the LED on
    delay(1000);
    digitalWrite(13, LOW);  
  }

}

shelleycat:
How are you going to test this?

Probably test it for a month or two and then hope the same principals apply for the year. Depending on where development goes, if it is vitally important that it works properly after a year I'd test it for a real year as well.

How are you going to supply the power for a year?

I was thinking a coin battery. The plan is to find some code that works in an Arduino prototype and then load it into MCUs for use in a standalone circuit.

What happens when the power goes off?

I'd research batteries beforehand to make sure the selected one will supply power for well over a year. If the power does go out, it'll fail.

Thanks for the suggested code!

I asked how you would test it as a serious suggestion - it obviously matters to you that it does work. So a suggestion would be to test with reduced times and much smaller delay constants. For example, a 4000 second delay would only take 40 seconds to test at a factor of 100.

I do not think that a coin battery would power an arduino MCU for a year unless you look at sleep and sleep timers. There have been examples in the forums - some people claim only 4 microamps for a sleeping MCU on its own.

Will the environment be benign or hostile to the battery and equipment?

You could also reduce power by not using an external oscillator but the lower frequency internal MCU oscillator I think.

It needs to be accurate only to within a few days of the target...

That's no problem, (assuming no software bugs, hardware failures, or loss of power with dead batteries).

A typical crystal has an accuracy of 100ppm (parts per million). So after 1 million days, you could be off by 100 days. Orone day after 10,000 days. (I think that works-out to around one hour per year.)

DVDdoug:

It needs to be accurate only to within a few days of the target...

That's no problem, (assuming no software bugs, hardware failures, or loss of power with dead batteries).

A typical crystal has an accuracy of 100ppm (parts per million). So after 1 million days, you could be off by 100 days. Orone day after 10,000 days. (I think that works-out to around one hour per year.)

What about ditching the crystal altogether (to simplify and reduce cost) and accepting a more substantial drift? I'd be OK with a drift of 3 days or so, depending on how much it reduces cost.

shelleycat:
I asked how you would test it as a serious suggestion - it obviously matters to you that it does work. So a suggestion would be to test with reduced times and much smaller delay constants. For example, a 4000 second delay would only take 40 seconds to test at a factor of 100.

I do not think that a coin battery would power an arduino MCU for a year unless you look at sleep and sleep timers. There have been examples in the forums - some people claim only 4 microamps for a sleeping MCU on its own.

Will the environment be benign or hostile to the battery and equipment?

You could also reduce power by not using an external oscillator but the lower frequency internal MCU oscillator I think.

The environment will probably be mostly benign - some temperature variation, but nothing too extreme. Dry.

I would probably be ok with leaving out the external oscillator, especially since I don't need it to be too accurate, relatively speaking.

I'll look into sleep and sleep timers. I had thought a coin battery would do the trick, but I haven't looked into it too thoroughly at this stage.

You could consider using a smaller micro.

You can get an ATTiny84/85 for cheap with an internal clock and not need any additional components to run it. They'll have lower power consumption. Using sleep modes, you can get down to the microamp range.

Arrch:
You could consider using a smaller micro.

You can get an ATTiny84/85 for cheap with an internal clock and not need any additional components to run it. They'll have lower power consumption. Using sleep modes, you can get down to the microamp range.

Yup, I was planning on using something along those lines. Something like that should be fine for a coin battery for a year, right?

Does it need to run on battery power for the whole year? If you can demote the battery to acting as a battery backup, the power consumption issue gets much easier to solve. Even just a set of AA batteries will get you well past any likely mains outage.

PeterH:
Does it need to run on battery power for the whole year? If you can demote the battery to acting as a battery backup, the power consumption issue gets much easier to solve. Even just a set of AA batteries will get you well past any likely mains outage.

Unfortunately, yes. It needs to be able to run for the entire year off of mains. Like a watch.
I wondered if a small solar panel would be useful for powering and charging the device and then have a backup battery for the times the solar panel/charged power source are depleted - but alas I know even less about those details.

cavallo:

Arrch:
You could consider using a smaller micro.

You can get an ATTiny84/85 for cheap with an internal clock and not need any additional components to run it. They'll have lower power consumption. Using sleep modes, you can get down to the microamp range.

Yup, I was planning on using something along those lines. Something like that should be fine for a coin battery for a year, right?

You could probably get a 200mAH battery to run a full year with a tiny using sleep modes. There's a few tutorials including one from Sparkfun that gives some info on getting ultra low power consumption out of these devices.