Introduction
This post, it will be shown how to ensure the reading of a digital input using our library Tools40. This library has been developed by Industrial Shields. For more information, check the information of this library in the following post:
Requirements
Ethernet or 20 I/Os PLC:Â Â Â Â Ethernet PLCÂ Â Â 20 I/Os PLCÂ Â Â Â
 Industrial Shields boards:   Industrial Shields Boards
 Tools40 IS library:          Tools40 Github repository
Description
First of all, it is important to know the purpose of our digital filter. Basically, it filters the debounces when a digital input changes from HIGH to LOW or vice versa. When the state of the pin is changing, during this change from HIGH to LOW for example the signal makes some debounces, so it can be read a wrong value that can provoke errors as you are reading a data that is not correct. The implementation of this library is based on a simple timer which ignores all the signals inside a period of time once he has read a different input than before. If we put a 200ms period of time and we read a LOW state, having before a HIGH state, the filter ignores during 200ms the input signal, after these 200ms it reads again and if it still a LOW state, the state of the input is changed to LOW. It is the exactly same idea for when it is changing from LOW to HIGH.
Example
Finally, it will be shown an example that filters the I0.8 digital input (Using a Digital Filter):
#include <Filter.h> // Create a filter // Filtering time: 200ms DigitalFilter<500> filter;
void setup(){ // Init Serial port Serial.begin(9600L); } void loop(){ // Read pin value int value = digitalRead(I0_8); // Filter it int filtered = filter.update(value); // Print the filtered value Serial.println(filtered); }
 This other example of the Filter.h library shows how to filter an analog input signal ( I0_10 in this example). As you can see, you must define the number of filters and also the sample period (in ms):
#include <Filter.h> // Create a filter // Number of samples: 10 // Sample period: 5ms (setting it to 0, samples are taken as fast as possible) AnalogFilter<10, 5> filter; //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { // Init Serial port Serial.begin(9600L); Â } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { // Read pin value int value = analogRead(I0_10); // Filter it int filtered = filter.update(value); // Print the filtered value Serial.println(filtered); }
How to ensure the reading of a digital/analog input library tools 40 digital filter