Skip to content

SensorSwitch::onInterrupt() doesn't reliably detect rising/falling edge #273

Closed
@liwenyip

Description

The issue is that there is a delay between the interrupt being triggered, and when this line of code executes:

if ( (_mode == RISING && value == HIGH ) || (_mode == FALLING && value == LOW) || (_mode == CHANGE) ) {

The delay is typically 2ms if SERVICE_MESSAGES is not enabled, and 200-1500ms if SERVICE_MESSAGES is enabled.

Say _mode==FALLING, and the pin receives a very brief pulse (e.g. a very brief push of a button); by the time this line of code runs the input may have gone high again, so (_mode == FALLING && value == LOW) will return false, and the method will interpret this as an invalid value.

My use case is a push button where I want to measure the length of time the button was pressed for. I've created a custom sensor that subclasses SensorSwitch; in the onInterrupt() method I need to be able to reliably tell whether the interrupt was triggered by a FALLING edge (button pushed) or a RISING edge (button released), even if the button was pushed so briefly that the pulse is only a few ms long.

I believe the only reliable way to detect whether a CHANGE interrupt was triggered by a RISING or FALLING edge is to read the pin as the very first instruction in the ISR (and store it somewhere for later use).

Apologies if that doesn't make sense (struggling with words today), happy to clarify if needed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions