Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

attachInterrupt() fails to detect falling edges when mode is set to CHANGE #68

Closed
@f-jiang

Description

@f-jiang

When I set my interrupt to detect both rising and falling edges using CHANGE, what ends up happening is that falling edges are ignored. I'm using a Nano 33 BLE.

My test setup consists of the code at the bottom, along with an LED connected to ledPin and a pushbutton connected to interruptPin. Note that interruptPin is pulled high.

What I expect to happen, given the CHANGE mode, is that LED should toggle when the button is both pressed (falling edge) and released (rising edge); however, it only toggles during button release.

(At first, I was having problems with my quadrature encoder code, but after some investigation it boiled down to the above issue).

const byte ledPin = 4;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions