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!

6
  • 5
    does pin 2 bounce? 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? 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.
    – Majenko
    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.
    – st2000
    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. 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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.