digitalRead not working on pins connected to interrupts - BUG?

Status
Not open for further replies.

lele

Member
Hi I have a VERY simple question. I need to read the status of a digital pin which is connected to an interrupt.

Without the interrupt this is straightforward:
Code:
status = digitalreadFast(pin);

but as soon as the interrupt is attached it just will no longer read the pin correctly.

If I disable the interrupt it will read it correctly again.

So there is definitely an issue between interrupts and digitalRead. Looks like a bug to me? Any other way to read the pin? This happened with the Teensy 4.1 and 3.5 units I have here, possibly with other models as well.

Pls let me know

Thanks!
 
hi, this is not working:

Code:
attachInterrupt (digitalPinToInterrupt (pin), onPinFalling, FALLING);
status = digitalreadFast(pin);

this is working:
Code:
detachInterrupt (digitalPinToInterrupt (pin));
status = digitalreadFast(pin);

thanks!
 
That is not a bad way to go. I often disable pin interrupts at the beginning of the interrupt service routine so that contact bounce does not cause multiple nested interrupts. At the end of the service routine, I re-enable that pin's interrupt. I often throw in a 20 ms delay to further reduce contact bounce. Also, since you set the interrupt to FALLING, there is no need to read the pin. You can just take the action for a LOW condition. You know it is low at the time of the interrupt because of the falling attribute.

Hope that helps,
Len
 
Sorry again still not really clear of what you are asking.... In what context?

Example this quick and dirty sketch does what I expect...
Code:
void onPinFalling() {
  Serial.println("Falling");
}

void setup() {
  while (!Serial && millis() < 5000) ;
  Serial.begin(115200);
  

  pinMode(0, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt (0), onPinFalling, FALLING);
}

uint8_t last_pin_state = 0x42;
void loop() {
  uint8_t pin_state = digitalReadFast(0);
  if (pin_state != last_pin_state) {
    Serial.println(pin_state);
    last_pin_state = pin_state;
  }
  delay(10);
}
 
you are right, it works now!

No idea what changed though, my code was pretty much identical to yours. Maybe there was an external cause there but I'm 100% sure I had this issue earlier where I could NOT read the pin correctly UNLESS I disabled the interrupts. I wasted lots of time in troubleshooting that.

So strange. I keep digging.

In the meantime thanks a lot to both for the help!
 
Status
Not open for further replies.
Back
Top