Pin Change Interrupt Library (PCINT) does not work on Arduino Mega

I did try to run a simple sample code from the Pin Change Interrupt Library (PCINT) library, but for some reason it did not trigger the interrupt.

My used hardwarea is an Arduino Mega with a RAMPS 1.4 shield and a RepRap Discount SmartController LCD including an encoder and button.

I actually wanted to create an LCD menu which get controlled by the encoder, but then I noticed that the encoder did not work. Tracing that problem back I found out, that it seems the problem comes from PCINT library which does not trigger the interrupt.

This is my test code (slightly modified sample code) for the library which does not work for some reason:

#include <Arduino.h>
#include <pcint.h>

// beeper on LCD PCB
#define BEEPER        37

// encoder on LCD PCB
#define BTN_EN1       31
#define BTN_EN2       33
#define BTN_ENC       35

// for RAMPS 1.4
#define X_MIN_PIN     3
#define X_MAX_PIN     2

#define Y_MIN_PIN     14
#define Y_MAX_PIN     15

#define Z_MIN_PIN     18
#define Z_MAX_PIN     19


#define led BEEPER
//#define btn X_MIN_PIN
//#define btn Y_MIN_PIN
#define btn BTN_ENC

void setled()
{
  // does NOT work
  digitalWrite(led, !digitalRead(btn));
}

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(btn, INPUT_PULLUP);
  PCattachInterrupt<btn>(setled, CHANGE);
  // setled();//initial led status
}

void loop()
{
  // works fine
  //digitalWrite(led, !digitalRead(btn));
}

I have tried the ports X_MIN_PIN (INT5), Y_MIN_PIN and BTN_ENC, but none of these work.
The beeper (led) works fine if I uncomment the line in the loop code.
Any idea what could cause this issue that the interrupt does not work?

Does the unmodified sample code work?

Is there an interrupt available on pin 35?

Do appreciate that not all pins on a mega support pin change interrupts.

The pins that support PCINTs on a Mega board are 0, 10-15, 50-53 and A8-A15.

aarg:
Does the unmodified sample code work?

I actually just modified the port pins and the two commented lines of code.

Danois90:
Is there an interrupt available on pin 35?

No, but as I wrote I also tested it with X_MIN_PIN (INT5) / pin 3 which has interrupt capability.

srnet:
Do appreciate that not all pins on a mega support pin change interrupts.

oqibidipo:
The pins that support PCINTs on a Mega board are 0, 10-15, 50-53 and A8-A15.

So far as I know has the ATmega2560 the possibility to trigger on almost all ports interrups, you just have to filter then the according pin by software if you have multiple pins masked, since there is then one interrupt for multiple pins.
I understand that the purpose if this library is to use interrups on all ports instead of just 2,3,18,19,20 and 21.

michael_uray:
So far as I know has the ATmega2560 the possibility to trigger on almost all ports interrups

The ATMega2560 datasheet says;

"The External Interrupts are triggered by the INT7:0 pin or any of the PCINT23:0 pins"

Is that not correct ?

Try with pin 10.

srnet:
The ATMega2560 datasheet says;

"The External Interrupts are triggered by the INT7:0 pin or any of the PCINT23:0 pins"

Is that not correct ?

Absolutely correct, I was wrong.

I picked that up there, but it did not notice that it was probably meant for a Atmega328 and not all ports of the ATmega2560 are covered by PCINTs.

..and that they're different than "pin change interrupts", which work on all (or nearly all) of the pins..

Now I understand that PCINT23:0 means a range of pins from PCINT0 to PCINT23, but it covers not all the pins on the ATmega2560.

BTW, there is a document which explains the megaAVR interrups in general pretty good.

Danois90:
Try with pin 10.

Pin 10 is assigned to heaters and fans mosfet on the RAMPS 1.4 so I cannot use it, but I tried it with the pin A9 and it worked fine there.

Thanks to all of you for all your help to improve my understanding how these pin change interrupts work.

It looks as if the library does not support to use INT7:0, this is the reason why it did not work with with X_MIN_PIN (3).
Not sure why it does not work with Y_MIN_PIN (14) since it is PCINT10, I did try it again, but without success.

However, I cannot use the given pins by the encoder (31/33) with these PCINs, means I have to find a solution to poll them somehow.