/*
// these definitions does in the main file
#define UNSIGNED_LONG_MAX (4294967295)
#define DEBOUNCE 5
unsigned long TimeDiff(unsigned long start, unsigned long finish) {
if (finish >= start) {
return finish - start;
} else {
// wrapped
return (UNSIGNED_LONG_MAX - start + finish);
}
}
*/
volatile unsigned long lastRise;
volatile unsigned long lastFall;
void isr_2() {
if (digitalRead(2))
lastRise = micros();
else
lastFall = micros();
}
void BounceInt2Setup() { // call this in Setup
lastRise = 0;
lastFall = 0;
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), isr_2, CHANGE);
}
bool Fell() { // Call from main loop(), to check for event
bool result = (lastFall > lastRise) && (TimeDiff(lastFall,micros()) > DEBOUNCE*1000) ;
if (result) {
lastRise = 0;
lastFall = 0;
}
return result;
}
try this
volatile int Button; // <<<<<<<<<<<<<<<<<<<<<<<<<< note the volatile before the declaration
// this forces the microprocessor to reload this variable each time it is used to make sure it is up to date
void isr_2()
{
unsigned long cTime;
static unsigned long EdgeTime;
unsigned long debounceTime = 10000; // adjust to meet your needs this is a guess
cTime = micros();
if ((cTime - EdgeTime) >= debounceTime ) {
EdgeTime = cTime;
Button++;
}
}
Z