attachInterrupt question

A program I wrote some time ago that uses attachInterrupt(interrupt, ISR, mode); where interrupt is the actual vector INT.0 and which is linked to digital pin 2 (which I setup as digital input).
This program succesfully ran for months, waking up the Atmege328 every 20 minutes on pin 2 (with DS3231 alarm and INT/SQW output), do its task and return to sleep.

Doing some browsing I find this reference attachInterrupt() - Arduino Reference discourages its use and recommends to use attachInterrupt(pin, ISR, mode); instead.

I never intend to run this program on anything else but Atmega168 or Atmega328. Would there be a reason to change the attachInterrupt to the recommended practice?

and recommends to use

It most certainly does NOT. Read the page again, and again, if needed, to see what the page DOES recommend.

Isn't the recommended syntax ...

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); (recommended)

?

Steve

Surely the safest option is

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

for 328 based boards as it removes any confusion as to whether the first parameter is a pin number or an interrupt number

On the page you linked to I can make no sense of this

attachInterrupt(pin, ISR, mode); (Not recommended. Arduino SAMD Boards, Uno WiFi Rev2, Due, 101 only)

UKHeliBob:
Surely the safest option is

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

for 328 based boards as it removes any confusion as to whether the first parameter is a pin number or an interrupt number

On the page you linked to I can make no sense of this

Hi, to PaulS too: you are right, I misread the recommendation, sorry!

I wonder why my program keeps running ok when using the actual interrupt instead of the pin number?

I wonder why my program keeps running ok when using the actual interrupt instead of the pin number?

Because nothing has changed

I wonder why my program keeps running ok when using the actual interrupt instead of the pin number?

Because the first argument to attachInterrupt() IS the interrupt number. The digitalPinToInterrupt() function (actually a macro) determines the correct interrupt number for any pin (or none, if the pin isn't an external interrupt pin) and returns that value, which attachInterrupt() then uses.

PaulS:
Because the first argument to attachInterrupt() IS the interrupt number. The digitalPinToInterrupt() function (actually a macro) determines the correct interrupt number for any pin (or none, if the pin isn't an external interrupt pin) and returns that value, which attachInterrupt() then uses.

How does the compiler know that the interrupt and not the pin number is used in the attachInterrupt command (third option on this page attachInterrupt() - Arduino Reference )?

How does the compiler know that the interrupt and not the pin number is used in the attachInterrupt command

It does not. What is knows is that the first argument to attachInterrupt() is supposed to be an 8 bit, unsigned integral value. You are the one that is supposed to be smart enough to make sure that the value you supply IS an interrupt number, or to use the digitalPinToInterrupt() function to convert a pin number that you know to the correct interrupt number.

brice3010:
How does the compiler know that the interrupt and not the pin number is used in the attachInterrupt command (third option on this page attachInterrupt() - Arduino Reference )?

The reason that you can get away with the attachInterrupt(pin, ISR, mode) syntax on certain boards (e.g. Arduino Zero) is that those boards happen to have interrupt numbers that match the pin numbers. But that is not the case with all boards and so this is why that syntax is not recommended.

UKHeliBob:
On the page you linked to I can make no sense of this

Would you mind providing some feedback on what you find confusing about it, or how it might be improved? I am doing a lot of work on the Language Reference pages right now and I welcome any input from the community on how that content can be improved. I will do my best to act on that feedback.

I've been having good success working with a volunteer administrator of the Language Reference content to get my less ambitious proposals reviewed and merged reasonably quickly and I feel like we're really making progress.

Would you mind providing some feedback on what you find confusing about it, or how it might be improved?

Certainly

attachInterrupt(pin, ISR, mode); (Not recommended. Arduino SAMD Boards, Uno WiFi Rev2, Due, 101 only)

This is very terse and was for me difficult to interpret simply due to the fact that the list of boards that it would work with is mixed in with the statement that it is not recommended leading to confusion as to which boards it could be used with A change to

attachInterrupt(pin, ISR, mode); (Not recommended).
This form of the command is only applicable to Arduino SAMD Boards, Uno WiFi Rev2, Due and 101 boards

Would make it much clearer

Thanks for your input UKHeliBob! Here's what I have proposed:

(an auspicious issue number: 555!)

Thanks for the feedback

555

Possibly the most used IC ever (so far)