Using RTC to run batch process

I have been searching for examples of this, but have found none so I figured I'd ask. I am trying to program an 8-hour batch process that will read a sensor to adjust the time to turn on relays each hour for the first 5 hours, then will wait 3 hours and take measurements and ultimately flip two more relays.

I found the TimeAlarm library on the forum, but that only handles 6 alarms and I have more events than that. Also, when an event happens, for instance a relay turns on, I read a sensor, then average the sensor and record that to an SD card, so there are many events tied to each action.

I thought of doing a series of if/then statements, where the if statement is about the time on the clock, where the hour is divided by 3 so as to repeat every 8 hours. Example if hour = 2, turn on relay for 5 min, record sensor, etc. The question is that if I go this route, is there any chance of a relay hanging?

Any advice would be helpful.

Thanks in advance!

How accurate does all this timing have to be?

Arduino can keep track of time by itself, but its crystal is not that accurate, so the time will drift off in a matter of days. A DS1307 based RTC has a more accurate crystal but will still drift off in a matter of weeks to months. A DS3231 based RTC is more accurate still and will take several years to drift off.

pekasus: I found the TimeAlarm library on the forum, but that only handles 6 alarms and I have more events than that.

It might be worth trying to get in touch with the author of that library or an expert on it. It might be really easy to amend the library to give as many timers as you want.

Aternatively, could you use fewer timers? Use them only for the "major" events, then have a sequence of subsequent events/actions timed by the Arduino code itself (see "millis()"). Or re-use timers by using them as once-only timers, rather than recurring, an have your code set the timer for the next event.

Alternatively don't bother with the library at all. Wire up the RTC to interrupt the Arduino every second. On each interrupt, read the time and have the code take appropriate action.

pekasus: Also, when an event happens, for instance a relay turns on, I read a sensor, then average the sensor and record that to an SD card, so there are many events tied to each action.

As I mentioned earlier, you could treat these "events" as simply timed actions kicked off by a single RTC timer. The Arduino's internal timer should be accurate enough for that.

pekasus: if I go this route, is there any chance of a relay hanging?

Yes, there is always that danger, whether you use timers or not. You could use a single-shot timer, set each time a relay is energised, to switch that relay off again after a suitable timeout.

Paul

Thanks for the reply, Paul!

I am planning on using the MCP79412 as it has very little drift and also has an onboard EEPROM that logs power outages.

I like your second comment. The library states that you can add more alarms, but each alarm takes up 12 bytes of RAM. Using that alarm sparingly for major events is probably the best route and would serve my purpose. The actual process doesn't have to be to the second accurate within itself...I just don't want the start time to drift too far over years. I think using the millis timer is better than the interrupt as it would be less likely to hang if the process gets interrupted by a button push or other event and fails to trigger the alarm for some reason. Does this sound right?

As for the relays, there are 3, so I could just repeat a digitalWrite LOW call on all three every 5 minutes when there is no activity.

Thanks for your input!