How does it work?
Learn how ptScheduler works.
Last updated
Learn how ptScheduler works.
Last updated
Under the hood, ptScheduler uses the native millis()
implementation of Arduino. The millis()
function is a timer based ISR (Interrupt Service Routine) that increments a global counter variable (32-bit unsigned integer) every millisecond. Calling the millis()
function will return the current value of the counter. Since Arduino implements the counter as a 32-bit unsigned integer, the counter will overflow every 49.71026962963 days. But ptScheduler is written with a workaround to overcome such overflow event.
When you create a new ptScheduler object, you can specify the time intervals and execution modes. The time interval is stored in a variable or in an array, and is compared to the time elapsed since the last time the task was executed. For example if the time period is 1000 milliseconds, the value is stored in a list called intervalList
and is compared to the elapsed time every time the associated call()
function is invoked. If the elapsed time is less than 1000 ms, call()
will return false. If the elapsed time is equal or greater than 1000 ms, call()
will return true. This will cause the code under the conditional clause will be executed only once. The next time you invoke call() and if the elapsed time is less than 1000 ms, the function will again return false. This kind of tasks where the code is executed only once every time interval is called a Oneshot task. This may not be how you want it. You might want to execute a task continuously for a specific time period and then sleep for the next cycle. It is possible with Spanning tasks. In addition to these basic types of tasks, ptScheduler allows you to alter many other behavior of tasks on the fly.
Let's look at detail how logic behind modes and timing work.
There are three properties that determine the task mode.
Whether a task is executed once every interval or executed repeatedly (continuously) during the interval.
Whether a task has equal or unequal intervals.
Whether a task is periodic or iterated (stops after specific number of interval cycles).
There are two types of tasks – oneshot and spanning. A oneshot task is executed once every recurring interval. See the graph below.
For example, every time a oneshot task is executed, you can toggle the state of an LED. This will blink the LED at every interval.
A spanning task is a one that is executed repeatedly during an interval. Such a task could be sampling a sensor continuously over a period of time. See the graph below.
During the Active time, the call()
function will return true
, and will return false
during the inactive time. Both the tasks shown above have a single interval value of Ti
Tasks are periodic or recurring and their intervals can be equal or unequal. For example, below is a spanning task with two unequal intervals.
The active time and inactive time intervals are different. ptSchdeuler allows you to set any number of intervals of any length. For example, something like below is also possible with 6 interval values of different lengths. Every interval set is called an iteration. This iteration or pattern can repeat as many times you want.
A task can run for either an indefinite period of time or stop after a number of iterations. You can set the number of iteration for a task using setIteration()
function. If no iteration was set (itearations
= 0), it will be a periodic indefinite task.
Depending on these properties, there can be 8 modes.
This example has a single interval value of Ti
.
Call setTaskMode(PT_MODE_EPO)
to use this mode.
2. EIO – Equal, Iterated, Oneshot
This example also has single interval value but it is iterated. After 5 iterations, the task will be either disabled or suspended.
Call setTaskMode(PT_MODE_EIO)
to use this mode.
This example has two intervals T1
and T2
.
Call setTaskMode(PT_MODE_UPO)
to use this mode.
This example has two intervals and the iteration is set to 2. After the two iterations, the task is suspended until you manually reactivate it.
Call setTaskMode(PT_MODE_UIO)
to use this mode.
This is a spanning task with equal intervals. Iteration is not set and so it repeats indefinitely.
Call setTaskMode(PT_MODE_EPS)
to use this mode.
This has iterations set to 4. Once four iterations are completed, the task will be suspended until you reactivate it again.
Call setTaskMode(PT_MODE_EIS)
to use this mode.
This spanning task has two unequal intervals T1
and T2
.
Call setTaskMode(PT_MODE_UPS)
to use this mode.
This spanning task has two unequal intervals T1
and T2
and is also iterated.
Call setTaskMode(PT_MODE_UIS)
to use this mode.
In addition to these, you can set a skip time or a pre-task delay. You can either express this delay in milliseconds, number of intervals or number of iterations. Ts
is the skip time.