The Threshold library implements a threshold for all-or-nothing control with Arduino. It is possible to use a single threshold or double threshold. The library uses Templates to work with different types of variables.
The threshold allows filtering a signal, activating or deactivating a state when the value crosses the thresholds. The following image shows the behavior with double threshold.
The library is suitable, for example, to activate a heat generator based on the recorded temperature, a pumping equipment when the level of a tank exceeds a certain level, turn on a light or alarm when a variable is higher (or lower) than a threshold.
Having a double threshold filters the noise, reducing the number of actuator activations. However, it is also possible to use the Threshold class for single threshold cases.
The hysteresis cycle used is positive, meaning the state is HIGH when the variable exceeds the RisingThreshold. You will have to be consistent with the actions used (for example, it is different to activate a heat generator, or a fan)
The class has an AddValue method to enter the new input state value. This way we can use the library with any input method, including GPIO, interrupts, calculated values, or received by any means of communication.
The library allows adding callback functions for the OnRising, OnFalling, and OnChanging events, so we can use the class itself to manage the program flow, without having to perform these tasks from the main loop.
For more information, see the entry All-or-nothing control with Arduino and threshold or double threshold.
The Threshold filter is instantiated through one of its constructors.
Threshold<T>(T threshold, bool state)
Threshold<T>(T lowThreshold, T highThreshold)
Threshold<T>(T lowThreshold, T highThreshold, bool state);
// Add a new value to the filter and return the state
bool AddValue(bool newState);
// Get the last state
bool GetState() const;
// Callback functions
The Threshold library includes the following examples to illustrate its use.
- SingleThreshold: Example of a single threshold
- DoubleThreshold: Example of a double threshold (hysteresis)
- Events: Example of callback functions
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE