Skip to main content
Post Undeleted by staad
Post Deleted by staad
deleted 36 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

interrupt 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 everytimeevery 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, striped downstripped-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-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!

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!

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 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!

added 406 characters in body; added 14 characters in body
Source Link
staad
  • 111
  • 4

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!

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.

Thank you for your help!

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!

Source Link
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.

Thank you for your help!