1
Beginners / Signal Edge (high and low) Validation and Counter - sharing Arduino/C++ Code
« Last post by Andre Laloni on Today at 02:19:30 am »I recently wrote a simple piece of code that could be helpful to others. Note that the code here needs a little customization to be used. I posted a generic version of it to facilitate such customization. Pretty self explanatory, however, please ask questions if needed. Hopefully it helps.
Best
AL
Best
AL
Code: [Select]
//set pins
const int pinOne = 2; //
const int pinTwo = 3; //
.
.
.
const int pinN = n; // define as many pins needed for as many input signals needed to be validated
//variables
double currentMillis; //the current time in milliseconds (begins count at power on)
unsigned long lastDebounceTime; // (miliseconds) the last time the signals were checked
unsigned long debounceDelay; // (miliseconds) the debounce time
//arrays
#define numSignals N //define array
// INDEX..............................[0].......[1].........[N]............
const int signalPins[numSignals] = {pinOne, pinTwo, pinN,...}; //include as many pins declared
boolean signalValidation[numSignals]; //validation output (1=valid)
boolean signalState[numSignals]; // current state of the signal
boolean lastSignalState[numSignals]; // previous state of the signal
int highSignalValidationCounter[numSignals]; // signal counter for valid high signals
int lowSignalValidationCounter[numSignals]; // signal counter for valid low signals
void setup() { //system variables initial set values
lastDebounceTime = 0;
debounceDelay = 10;//change to suit sensitivity needs
//arrays initial set values
for (int i = 0; i < numSignals ; i++) {
signalValidation[i] = 0; //validation output (1=valid)
signalState[i] = 0; // current state of the button
lastSignalState[i] = 0; // previous state of the button
highSignalValidationCounter[i] = 0; // signal counter for valid open or close signals
lowSignalValidationCounter[i] = 0; // signal counter for valid open or close signals
}
}
void loop() {
currentMillis = millis(); //get the current millis at the beginning of the loop
signalVal (); //validates input signals
}