Skip to content

[Reference] attachInterrupt() example and documentation corrections #4723

@per1234

Description

@per1234

On the reference page https://www.arduino.cc/en/Reference/AttachInterrupt the example attaches the interrupt to pin 13 but that won't work for common Arduino boards such as Uno and Mega. Also I think it's better to use different pins for the interrupt and the output. I would suggest changing the interrupt pin to 2 or 3 as this will work on any of the listed boards except MKR1000 Rev.1. The updated example would look something like:

const byte ledPin = 13;
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;
}

Originally reported at http://forum.arduino.cc/index.php?topic=385081.0 and https://forum.arduino.cc/index.php?topic=358930.0

A couple other typos in the attachInterrupt() reference page:

Normally you should use digitalPinToInterrupt(pin), rather than place interrupt an number directly into your sketch.

should be changed to:

Normally you should use digitalPinToInterrupt(pin), rather than place an interrupt number directly into your sketch.

and:

Four contstants are predefined as valid values:

should be changed to

Four constants are predefined as valid values:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions