1

Please examine the following code.

volatile uint8_t myVar;
void setup() {
 attachInterrupt(digitalPinToInterrupt(2), isr, FALLING);
 Serial.begin(115200);
}
void loop() {
 myVar = 0;
 while (myVar != 5) {
 }
 Serial.println("ISR called");
}
void isr() {
 myVar = 5;
}

From this code, I expect that every time the pin 2 is falling, I get one single print statement ISR called.

However, when dropping the voltage on pin 2 ONCE, the serial monitor shows ISR called TWICE.

Why is this? This is a simplified, stripped-down example of the problem I am facing with my current project.

Edit: After reading the comments, here is a bit more info. I am using a 64-button shield that is triggering the interrupt. The process is time critical since I am measuring the time it takes for the button to be pressed (I am testing people's reflexes). I will add a print statement in the interrupt to see how many times it gets called. However, I believe the interrupt is only getting called once per buttonPressed.

Thank you for your help!

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Oct 19, 2017 at 12:15
6
  • 5
    does pin 2 bounce? Commented Oct 19, 2017 at 12:27
  • 1
    It looks like its bouncing. How are you 'dropping the voltage' ? I don' t know if it could be this but could it be that the ISR is being called twice because as it drops from 5V to say 3V it fires once, and then it drops from 3V to 0V and fires again. Could you try a different trigger? Commented Oct 19, 2017 at 12:29
  • 3
    @CodeGorilla the Schmitt trigger should stop that kind of thing. It would trigger once at 1.6v and have to rise back to 3.3v before it triggered again. Commented Oct 19, 2017 at 13:08
  • As a quick check, I would have suggested adding a delay to account for the switch debouncing. But since you've decided upon using an interrupt instead of polling (why? do you really need to respond that fast?!) you are now facing the difficulties associating with interrupting your programming at any point. (Possibly) Even in the middle of a delay. Many programmers (even people who make a living at this) have a difficult time wrapping their heads around this paradigm. Commented Oct 19, 2017 at 15:54
  • "I will add a print statement in the interrupt" = very bad idea. You can count in interrupts, but don't print. Commented Oct 20, 2017 at 5:27

1 Answer 1

0

Turns out, the 64 button shield calls the interrupt TWICE. Once when the button is pressed and once when it is released.

Thank you for everyone's help!

answered Oct 19, 2017 at 17:09
6
  • Accept your own answer to close the question. Commented Oct 19, 2017 at 22:21
  • @LookAlterno I would if I could. Stack exchange forces me to wait a day. Commented Oct 20, 2017 at 1:16
  • 1
    This is probably not the whole story. You may have inadvertently misconfigured interrupts for both edges. Commented Oct 20, 2017 at 5:29
  • It would be nice if you included what change you made to solve the problem. Commented Oct 20, 2017 at 10:51
  • @RubberDuck I don't have the Arduino with me right now but when I do I will. Commented Oct 20, 2017 at 12:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.