How to develop a user defined scheduler?

Thread Starter

Rishi0

Joined Jun 21, 2022
1
I am trying to develop a system where an activity will be triggered at the time set by the user. I am not too concerned about the activity right now, it can be turning on an LED, starting a motor, or something else. I would like to have some advice on the scheduling system. How can I make a system where the user can set a time for an activity? To be clear, it's not like setting a delay. It's more like setting a specific time (x:yz am/pm)

I'm fairly experienced with Arduino, but feel free to share links to similar work with other microcontrollers. I should be able to translate them into Arduino.

I appreciate your help!
 

ApacheKid

Joined Jan 12, 2015
720
I am trying to develop a system where an activity will be triggered at the time set by the user. I am not too concerned about the activity right now, it can be turning on an LED, starting a motor, or something else. I would like to have some advice on the scheduling system. How can I make a system where the user can set a time for an activity? To be clear, it's not like setting a delay. It's more like setting a specific time (x:yz am/pm)

I'm fairly experienced with Arduino, but feel free to share links to similar work with other microcontrollers. I should be able to translate them into Arduino.

I appreciate your help!
You can begin by thinking about a linked list (which could be just an array if you wanted) of things to do, ordered by the earliest first. As the user submits requests to do this or that at such and such a time, you create a node representing that and insert it into the list at the appropriate position.

Each time an item is inserted you adjust or set a timer to expire at the time of the next event (the head of the list), If the insertion does not change the head of the list then you leave the timer as it is.

When the timer expires it removes the head of the list and does the work represented by that removed node and also resets the timer to expire at the time corresponding to the new head of the list (the node that previously was right after the head). Depending on the specifics of your platform you might need to be mindful of race conditions (like a timer expiring at the exact same time as a new user request being inserted).

If you work that through in an abstract sense, say with a whiteboard you'll start to get an outline of the logic. The model can also be adapted to support repeating activities as well as interval rather than absolute time based activities, you can also design in the ability to remove (cancel) an activity from the list.

Get this all clear and solid on a whiteboard before writing a single line of code too, the more you can debug this on a whiteboard the less you'll have to debug it at the keyboard!
 

click_here

Joined Sep 22, 2020
544
What you need to research is something called an "observer pattern".

Make a time source the "subject" and the tasks that you need done as the "observers"

There is a really good example in part 4 of "Patterns in C" by Adam Petersen
 

John P

Joined Oct 14, 2008
1,978
My usual method is to set up a repetitive timer interrupt at some rate that's faster than anything you need to do, maybe 10KHz. Then in the interrupt, visit each of several variables in turn, and if they have a value of > 1, decrement them.

Then back in the main() routine, or the loop() on an Arduino, when there's a need to perform a task some time in the future, I set one of those variables to whatever value corresponds to the desired time, say 1000 for a 1 second delay if the timer runs at 10KHz. The main() function loops through these variables, and when any of them is found to have the value 1, the associated task is performed and the variable is set to 0. Or, if the task actually has to be done repetitively at the same rate, set the value back to 1000 or whatever was used before.

You can do this for any number of tasks at any rates you need, depending on how much processing power you have and how much needs to be done. But it's not absolutely accurate, especially if you get the "jackpot condition" where everything needs to happen at once! To get pinpoint accuracy (well, actually not, but better) you need to perform the tasks within the interrupt and not in main(), but that's something to look at carefully, because you need to keep the time in the interrupt as brief as you can.

Edit to say: The variables used for the countdowns have to be global, because they're accessed by both main() and the interrupt.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
8,390
I perform scheduling by adding the delay between the execution of each task to the current time in milliseconds, either at the beginning or end of the task. I save this in a structure for the tasks in a variable representing time of next execution. When the current time is greater (or equal) to the next execution time, I execute the task. This solves the problem of many tasks executing at the same time by cooperatively allowing each task to run. This approach is NOT appropriate when the execution times are critical. But it does work in many situations.
 
Top