Hi all,
I've been playing with a basic interrupt example, connecting two buttons to PIN2 and PIN3 respectively. Using the code below the wrong interrupt sometimes fires when I put a capacitor across each button (pressing the button connected to PIN2 fires the interrupt associated with PIN3, and vice versa). In fact, I has hoping to avoid the "if (thisItr - lastItr > ITR_DELAY)" checks altogether by adding the capacitors.
Any suggestions for how to do this properly would be appreciated!
#define PIN_LED 13
#define ITR_DELAY 200
int ledState = LOW;
int itrCountAccept0 = 0;
int itrCountIgnore0 = 0;
int itrCountAccept1 = 0;
int itrCountIgnore1 = 0;
unsigned long lastItr = 0;
unsigned long thisItr = 0;
void setup() {
pinMode(PIN_LED, OUTPUT);
attachInterrupt(0, itr0, RISING);
attachInterrupt(1, itr1, RISING);
Serial.begin(9600);
}
void itr0() {
thisItr = millis();
if (thisItr - lastItr > ITR_DELAY) {
lastItr = thisItr;
itrCountAccept0++;
ledState = !ledState;
}
else {
itrCountIgnore0++;
}
}
void itr1() {
thisItr = millis();
if (thisItr - lastItr > ITR_DELAY) {
lastItr = thisItr;
itrCountAccept1++;
ledState = !ledState;
}
else {
itrCountIgnore1++;
}
}
int count = 0;
void loop() {
digitalWrite(PIN_LED, ledState);
delay(100);
count++;
if (0 == count % 5) {
Serial.print(itrCountAccept0);
Serial.print(" ");
Serial.print(itrCountIgnore0);
Serial.print(" ");
Serial.print(itrCountAccept1);
Serial.print(" ");
Serial.println(itrCountIgnore1);
}
}