The Countdown library implements a simple counter. The counter is initialized to a certain value using the StartValue variable. The current value of the counter is saved in the Value variable.
The counter has the Tick() method, which reduces the value of Value until it reaches zero. It also has the Reset method, which resets Value to the StartValue value.
Optionally, there is a callback function called OnFinish, which triggers when the counter reaches zero.
Apart from using a simple counter, such as a lap counter or an encoder, the main interest of the library is to use the Callback function to free up part of the code from the main flow of the program.
For example, we can use the counter to ignore the first N cycles of a signal to wait for it to stabilize, or to trigger a function every N occurrences of an event.
Finally, the functionality of the library is more interesting if we combine it with other libraries such as AsyncTask, MultiTask, StateMachine, or PetriNet.
The Countdown class is instantiated through one of its constructors.
Countdown(uint16_t startValue, CountdownAction OnFinish);
// Initial value of the counter
// Current value of the counter
// Decreases the value of Value and triggers OnFinish if Value == 0
// Resets the value of Value to StartValue
// Callback function triggered when Value == 0
The Countdown library includes the following examples to illustrate its use.
- Countdown: Example of using the Countdown class
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE