Please advice, I need to make a device that triggers a light and a siren at set times. This time can be different for each day.
How to save those times?
I’m thinking about a database, but I don’t find it wise to read the database every 30 sec and check if that minute the current time corresponds to some set time.
The accuracy of alarms is a minute interval.
Here's one possible approach:
Store the alert times in a database.
Access the database once or twice per day (assuming that database updates do not happen often) and populate a list of alarms.
Then, every 30 seconds you can check this list, instead of the database.
You don't need to check the database, or a list, every 30 seconds. Here is an approach that I used for a 'phone alarm app ...
Sub setTimerPeriod
- Set the time period to 30 minutes or 40 minutes (or any arbitrary interval).
- Check each alarm entry in the database against the current time.
- If the alarm is overdue then raise it now.
- Otherwise, if the time interval between now and when the alarm is due is less than the current timer duration then set the timer duration to this value plus one second.
That's all. The effect of this code is to set the timer to fire one second after the next alarm becomes due. If there are no alarms in the outlook period then the database will be scanned again in 30 (or 40) minutes. Call this routine from the timer event. Call this routine when the application starts and call it whenever a new alarm is added (or removed) from the database.