Skip to main content
2 of 3
added 406 characters in body; added 14 characters in body
staad
  • 111
  • 4

interrupt volatile variable doesn't work as expected

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 everytime 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, striped 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!

staad
  • 111
  • 4