I am currently working on a project and wanted to get some advice on the best type of code implementation.
- Customer selects time period they want in hours (1, 3, 6, 9, 12, 24etc)
- Customer chooses device they want to use this on (8-16 devices)
- Relay switches on for device selected and countdown starts
- Relay switches off when countdown (hours) are over
- Multiple customers can use multiple devices at the same time with different length timers
- Timers must be robust and accurate (~1min in 24hours)
- Timers must survive reboot/power outage and continue counting down
- Have ability to survive power outage/reset and continue
I have a basic working implementation with an Uno, a coin acceptor to add credits of hours, a switch to select the device and a single relay. I am currently doing the timer by taking the Millis() when the button is pressed, creating FinishTime in Millis by adding on the hours the user selected and then checking in the loop if the current Millis() is between these 2 numbers (then relay ON, otherwise OFF).
But this approach will not survive resets and is probably not scalable to 8+ concurrent timers. I also need to be able to handle new customers setting new timers (via interrupt from Coin acceptor pulse) and displaying things on LCD at the same time as not compromising the ongoing timers. I am a bit concerned about writing too much to the EEPROM so would be happy to write the countdown state/time remaining maybe every minute?
I have seen lots of different implementations of timers but they all tend to be counting up, using Delays or not working for multiple timers in parallel. I could use a RTC as well and set actual finish times, but concerned this is another thing to go wrong and could cause incorrect timing if it failed.
Priority here is the non-failure of the timers and would be willing to trade off some accuracy (in the customers favour) to achieve this.
Really appreciate any help or advice anyone can give in terms of code implementation, approach or libraries.