Hello,
I am having a weird problem with the Leonardo mini board and using INT 2 and INT 3.
The problem is I attach an interrupt handler to INT 2 and to INT 3. But when the interrupt fires on INT 2 it also calls the interrupt handler attached to INT 3, even when I completely detach the input sensor from the assigned pin for INT 3.
Yet, it does not do this on the Duemilanove. There, it works fine with the interrupts.
Below is sample code that will activate when either switch is fired. Ideally each switch fires only once per revolution. But on the Leonard you get two activations on a single switch per revolution. One reports the correct time, the other always shows a pulse time of 0. The pulse time of 0 happens at the same moment the first interrupt handler is activated.
The circuit used is simple simple. Two reed switches on either pins 2/3 on the Duemilanove or Pins 0/1 on the Leonardo. Other side to ground.
On each revolution you get switch 1 activated, then switch 2. The triggers are timed and debounced.
// Interrupts
// For Duemilanove
//#define INPUT1_INTERRUPT 0
//#define INPUT2_INTERRUPT 1
// For Leonardo
#define INPUT1_INTERRUPT 2
#define INPUT2_INTERRUPT 3
// PIN Assignments
// For Duemilanove
//#define INPUT1_PIN 2
//#define INPUT2_PIN 3
// For Leonardo
#define INPUT1_PIN 0
#define INPUT2_PIN 1
#define DEBOUNCE_DELAY 50
long int gblInput1Debounce = 0;
long int gblInput1Start = 0;
long int gblInput1Stop = 0;
long int gblInput1TotalPulseTime = 0;
long int gblInput2Debounce = 0;
long int gblInput2TotalTime = 0;
void setup() {
Serial.begin(9600);
Serial.println("Initializing");
// Sets our digital pin
pinMode(INPUT1_PIN, INPUT);
// Turns on internal pull-up resistor.
digitalWrite(INPUT1_PIN, HIGH);
// Attach our interrupt and interrupt handler
attachInterrupt(INPUT1_INTERRUPT, input1Count, FALLING);
// Sets our digital pin
pinMode(INPUT2_PIN, INPUT);
// Turns on internal pull-up resistor.
digitalWrite(INPUT2_PIN, HIGH);
// Attach our interrupt and interrupt handler
attachInterrupt(INPUT2_INTERRUPT, input2Count, FALLING);
}
void loop() {
// Testing
}
void input1Count()
{
// debounce and increment timers.
if ((millis() - gblInput1Debounce) > DEBOUNCE_DELAY)
{
gblInput1Debounce = millis();
gblInput1TotalPulseTime = millis() - gblInput1Start;
gblInput1Start = gblInput1Debounce;
Serial.print("Input1 Pulse Time:");
Serial.println(gblInput1TotalPulseTime);
}
}
void input2Count()
{
// debounce and increment timers
if ((millis() - gblInput2Debounce) > DEBOUNCE_DELAY)
{
gblInput2Debounce = millis();
gblInput2TotalTime = millis() - gblInput1Start;
Serial.print("Input2 Total Pulse Time: ");
Serial.println(gblInput2TotalTime);
}
}
When you run the code on the Leo you get this:
Input1 Pulse Time:5673
Input2 Total Pulse Time: 0
Input2 Total Pulse Time: 163
Input1 Pulse Time:356
Input2 Total Pulse Time: 0
Input2 Total Pulse Time: 174
Input1 Pulse Time:390
Input2 Total Pulse Time: 0
Input2 Total Pulse Time: 196
Input1 Pulse Time:433
Input2 Total Pulse Time: 0
Input2 Total Pulse Time: 215
When you run it one the Duemilanove you get this:
nput1 Pulse Time:228
Input2 Total Pulse Time: 115
Input1 Pulse Time:219
Input2 Total Pulse Time: 126
Input1 Pulse Time:243
Input2 Total Pulse Time: 140
Input1 Pulse Time:264
Input2 Total Pulse Time: 154
The above is correct. One pulse per revolution. The leo is incorrect. I'm getting two pulses per revolution, and one of them (the one at 0ms) fires exactly when the first interrupt handler activates.
Bonus problem: Even when I disconnect the input2 wire, the input1 interrupt handler will fire off the input2 handler at the same time. Again, with nothing attached to that pin/interrupt combo.
Any help is appreciated. I'm out of options and maybe I'm overlooking something obvious.
I have tried the above code with IDE 1.0.2, 1.0.3 and 1.5.2 Beta. All the same results.