Tasks

Introduction

If you need to perform any kind of background processing task, then you will need to consider how to break it up so it executes in small slices to allow other system functions to continue normally.

You can use callback timers to schedule periodic tasks, but if you simply need to run the task as soon as possible you should use the Task Queue.

The BasicTask class

This class uses the task queue plus a timer to provide an easy way to write a background task. All you need to is define a loop() function which does the work. The task has three states:

Task states

To see this in operation, have a look at the Basic Tasks sample.

Task Schedulers

If you want to read further about schedulers, there are various libraries available for Arduino.

These are quite simple and generic:

This one is rather more complex:

Using a scheduler is a powerful technique which allows the programmer to focus on the task at hand, rather than how to get it to run.