Arduino Tips Number 3: Scheduling tasks

Often an Arduino sketch needs to do things at regular time intervals. For example, my home automation system has data collection boards that record temperature, humidity, light and motion. This can be done using the built in delay function, but everything halts whilst you wait for a delay or the millis function. This all gets a bit complicated, but there is an easier and much more convenient way by using the timer library. This simple bit of code demonstrates how it works in my home automation system. We start by importing the library and setting up timer variable. In the setup function we call the timer.every function with arguments 2000 (in milliseconds) and findTemp. This will call my function findTemp (defiened later in the code), which contains all of the gubbins to read my temperature sensors. This will be called every two seconds. In the loop function we call the timer.update function which will repeatedly check the time to determine if a timer.every needs to be called.

In my home automation code I define several different timer.every functions to call different functions for example to read smoke detectors, light sensors, motion sensors and humidity sensors. They are all timed at different intervals for different uses.

Comments