Skip to content
Michael Miller edited this page Mar 25, 2016 · 2 revisions

(actively under construction)

Tasks are very flexible things. They maybe something as simple as blinking an LED, or as complex as handling WiFi protocol communications. The idea is to break down features of your sketch, then break down those features into functional tasks. Exactly how granular you do this is your decision.

The first rule of writing tasks is never use delay(). The delay() function will stop all other processing for the period of time you pass to it. This means you are wasting your CPU time just spinning waiting on time to pass while you could be doing other things.

When examining your code for putting into tasks, examine first the top level features. Do you have features that are always active? A blinking status LED is one example. Reading a temperature sensor is another. Do you have features that are active sometimes and inactive at other times. An alarm siren is an example here; where most of the time its inactive but when an alarm is happening, it becomes active.

Then consider periodic functions. Some things you only want to do a set schedule. Reading that temperature sensor is another good example here. The room temperature doesn't change very fast, so only reading it once every 10 seconds is plenty fast enough.

This walk through will cover creating a blinking light task that could be reused in your projects.

This walk through will cover creating a button task that could be reused in your projects. This will take the current ButtonTask example and break it down even further. Thus showing that current "state" may expose locations for splitting code further down into tasks.

Clone this wiki locally