I have isolated a problem I am having with interrupts on the Due board to the below code:
volatile boolean a = false;
volatile boolean b = false;
void setup() {
int a_pin = 51;
int b_pin = 49;
Serial.begin(9600);
Serial.println("0");
attachInterrupt(a_pin, a_func, RISING);
Serial.println("1");
attachInterrupt(b_pin, b_func, FALLING);
Serial.println("2");
}
void loop(){
Serial.println("3");
if(b==true){
Serial.println("b true");
delay(750);
}
Serial.println("4");
if(a==true){
Serial.println("a true");
delay(750);
}
Serial.println("5");
}
void a_func(){
Serial.println("a interrupt");
a = true;
}
void b_func(){
Serial.println("b interrupt");
b = true;
}
The above code is supposed to work in conjunction with the simple circuit here. :
I am confident the button is properly debounced, verified through the oscilloscope.
From the circuit, the normally high output of the voltage divider goes to pin 49 (associated with 'b' boolean and function) on the Due which is set to receive interrupts from FALLING signals (i.e. HIGH to LOW). The capacitor is to filter out any noise. The interrupt prints 'b interrupt' and sets 'b' = true, which causes the print statement 'b true' to print repeatedly in the loop().
The problem is that the interrupt is happening before the button is pushed, i.e. pin 49 must see a FALLING signal before I actually do anything. To determine the order of events I added a number of print statements and the output is as seen here:
0
1
b interrupt
2
3
b true
4
5
3
b true
4
5
As you can see, the 'b interrupt' happens between the prints of '1' and '2', setting 'b'=true, causing the 'b true' statement to print. Pushing the button associated with 'b' causes the 'b interrupt' print statement as expected, but I do not understand why the interrupt initially occurs before I physically do anything. I have watched the voltages on pin 49 with an oscilloscope to check if the pin defaults to HIGH and then goes low on program execution (thus triggering the interrupt), but as far as I can see, the pin always reports ~2.5V, unless I push the button, which causes it go to 0V as expected.
I added the interrupts associated with 'a' and simply grounded pin 51. I have no problems with the interrupt happening unexpectedly, as seen in the Serial output by lack of statements related to 'a'.
Is this some kind of weird system thing where the interrupt is called when I attach a function with a FALLING mode or am I getting a falling signal I can't see?
Update: I connected pin 51 to a normally closed debounced button in a similar circuit to the one in the original post. This means the output of the voltage divider is normally LOW and goes to HIGH on button push. Attaching an interrupt on such a RISING logic change does not produce the problems seen in the original post and the interrupt never occurs unless the button is actually pushed. While I realize I could just always use a normally closed button and use RISING interrupt triggers, it would still be nice to know what is causing my original problem.