Connecting Interrupt switch to Arduino Mega SCL 21 pin

Hi,

I'm new to Arduino and am having some trouble connecting an interrupt switch to any of the following pins:

TX1 18, RX1 19, SDA 20, SCL 21.

I'm trying to connect to these pins because I saw that they were interrupts 2, 3, 4, and 5 respectively.

SCL 21 is the pin I am currently trying to connect to. I have the wiring correct since I tested it with pin 2 (Interrupt 0) and it does what it is supposed to, I just can't get it to work on the SCL 21 pin.

The code I'm using id the following:

int button1 = 21;

void setup() {

Serial.begin(9600);
attachInterrupt(2,attempt1,FALLING);
pinMode(button1, INPUT);
}

void attempt1()
{.....
}

Is 21 the right way to call out the pin? Is 2 in the attachInterrupt command correct since SCL 21 is interrupt port 2? I want to use the pins above since pins 2-13 are already in use. Thank you!

Use the 'digitalPinToInterrupt() ' function to match your pin to the correct interrupt

const int button1 = 21;

void setup() {

 Serial.begin(9600);
 attachInterrupt(digitalPinToInterrupt(button1),FALLING);
 pinMode(button1, INPUT);
}

void attempt1()
{
  //.....
}
[code]
attachInterrupt(digitalPinToInterrupt(button1), attempt1, FALLING);

No, it is a Mega. I included that in the title but forgot to iterate it in the post. I have a string of 11 LED's plugged into pins 3-13. They light up from left to right with a red light in the center and the goal is to press a button to stop (interrupt) the sequence on the red light and receive a score. I have two pushbuttons, one to stop the lights and one to cycle through LED sequencing speed. The first pushbutton is wired to pin 2 (Interrupt 0). Since the LED's use the rest of the pins I want to wire the second pushbutton to a different interrupt pin. There is also an LCD (wired to pins 24, 25, 26, 27, 28, and 29) that displays a score based on the first pushbutton and difficulty based on the second button. Since I am out of pins 2, 3 (interrupt 0,1), I am trying to figure out how to wire a second interrupt for the second pushbutton using the pins stated in the original post. I have verified that all of the code works except for what I have posted. I do not know how to call out pin SCL 21 in code and get that pin to function as an interrupt. Any help is greatly appreciated. Thanks!

Thanks all!

Unfortunately, using digitalPinToInterrupt has no change. I'm using the following:

const int button1 = 21; //button used in pin 21

void setup() {

attachInterrupt(digitalPinToInterrupt(button1),difficulty,FALLING); //interrupt for button 2
pinMode(button1, INPUT); //sets the pin used by button 1 to input mode

}

void attempt1(){
}

I have tried:
Switching FALLING to RISING
Pins 20, 19, 18
Writing in "21" instead of "button1"
Swapped out all my wires, resistors, and pushbuttons
Resetting the Arduino

I'm stumped

Functional check of INT0 interrupt of MEGA.

1. This is (Fig-1) the hardware setup.
intzMega.png
Figure-1:

2. Upload the following sketch (tested in MEGA):

volatile bool flag = false;

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  pinMode(21, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(21), ISRZ, FALLING);
}

void loop() 
{
  if(flag == true)
  {
    delay(2000);
    digitalWrite(13, LOW);
    flag = false;
  }
}

void ISRZ()
{
  digitalWrite(13, HIGH);
  flag = true;
}

3. Place one end of a jumper wire (K1 in Fig-1) at DPin-21 (SCL).

4. Gently, just momentarily touch the other end of the jumper wire with the GND-pin of MEGA.

5. Check that L has blinked with 2-sec ON-period.

6. Repeat the process.

7. This is the end of the functional test of INT0 interrupt of MEGA.

intzMega.png

awkid:
Thanks all!
Unfortunately, using digitalPinToInterrupt has no change.

So, according to the documentation, if button1 = 21...

attachInterrupt(digitalPinToInterrupt(21), attempt1, FALLING);
attachInterrupt(digitalPinToInterrupt(button1), attempt1, FALLING);
attachInterrupt(2, attempt1, FALLING);

are equivalent statements. The benefit of the middle line over the other two is that you can refactor the code for a different interrupt pin simply by changing the value of button1.
So, probably need to do some simpler tests like GolamMostafa suggests. a series of print statements to check if

  1. you enter the interrupt on rising or falling or at all
  2. swapping out the interrupt for anything else, does the code execute?
    I think your problem is not in the code you are posting, but the code you aren't sharing.

Floating input pin?