token-vesting icon indicating copy to clipboard operation
token-vesting copied to clipboard

attachInterrupt, a big hole in documentation and no example

Open PM04290 opened this issue 1 year ago • 1 comments

For several days I have been struggling with an ATTiny3216 to understand why in standby, when I use attachInterrupt I have 200µA of consumption when I should only have 2µA. In fact I was using the function like this: attachInterrupt(PIN_PA5, myFunc, FALLING) while it should be used like this: attachInterrupt(digitalPinToInterrupt(PIN_PA5), myFunc, FALLING) the first parameter is named "pin", for me it seemed logical. the documentation should be improved and an example put

PM04290 avatar Oct 19 '24 07:10 PM04290

I am not sure that Attachinterrupt is MegatinyCore specific. It seems a generic Arduino function that has it's own reference documentation overhere

It has an example, including the mentioned "digitalPinToInterrupt"

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;
}

hmeijdam avatar Oct 19 '24 11:10 hmeijdam

attachInterrupt(PIN_PA5, myFunc, FALLING) (or more generally attachInterrupt(pin_number, myFunc, FALLING) is never correct; it only happens to work some places on some parts when pin_number=digitalPinToInterrupt(pin_number). These places happen to be common on classic AVRs, though this was by chance not design. Notice how the real interrupts work on every pin, unlike classic AVRs)

SpenceKonde avatar Dec 06 '24 06:12 SpenceKonde