Which Interrupt pins can I use

Hello I am trying to use the interrupt pins to use with buttons on a project I am working on. I am using the Arduino mega 2560 rev 3. I have read through the Arduino docs and it it says for the Arduino that for that board pins 2, 3, 18, 19, 20, and 21. I am curious if anyone knows if I can assign digitalPinToInterrupt() function to other pins for this board.

No. You cannot decide that an arbitrary pin is associated with a hardware interrupt

However, you may want to investigate pin change interrupts

Are you sure that you need to use interrupts of any type ?

Usually there is no need for an interrupt for a button.
Use one of the available libraries. For example "OneButton".

Is there some reason you don't believe the official documentation?

Correction they are limit switches and I would like is the limit switch gets pressed I want my code to stop what it is doing.

So, once in the ISR, how will you accomplish that?

1 Like

for(;;); :rofl:

Still no need for an interrupt.
Check the state of your limit switches with digitalRead,
and continue your task when the criterion is not fullfilled.

void setup() {
  Serial.begin(115200);
  for (int ii = 0; ii < 54 ; ++ii) {
    int intNum = digitalPinToInterrupt(ii);
    if (intNum >= 0 ) {
      Serial.print(ii);
      Serial.print(":");
      Serial.println(intNum);
    }
  }
}

void loop() {
}
2:0
3:1
18:5
19:4
20:3
21:2

Nope. That looks like it.

1 Like

thank you

As UKHeliBob stated, you have to use the predefined Pins as specified in the doc. The digitalPinToInterrupt() function is there to make your code a little more readable (if you prefer) as you use the interrupt Pin # (as defined in the doc), rather than use the built in Interrupt Vector definition.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.