Interrupts not working (solved)

Hi all,

I'm having problems with the interrupts with my Arduino Uno Wifi Rev 2.

This code works:

const byte ledPin = 25;
const byte interruptPin = 2;
volatile byte state = HIGH;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
}

void loop() {
  digitalWrite(ledPin, state);
  state = digitalRead(interruptPin);
}

to turn off the LED, I connect digital pin 2 to ground.

This code does not work:

const byte ledPin = 25;
const byte interruptPin = 2;
volatile byte state = HIGH;

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

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

void blink() {
  state = !state;
}

No matter what I do to digital pin 2, the LED will not turn off.

Any thoughts?

Thank you,
Darren Chapman

This code works:

const byte ledPin = 25;
const byte interruptPin = 2;
volatile int state = HIGH;

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

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

void blink() {
  state = !state;
}

The problem was the syntax in the attachInterrupt().

Thanks,
Darren

1 Like

I'm glad to see you found the solution. Thanks for taking the time to post an update.

FYI, the first code will work with the next release of the Arduino megaAVR Boards package used for the Uno WiFi Rev2: