Arduino Mega not all interrupt pins working (solved)

Hey folks,
I have an encoder in form of a hall sensor. It works when I use the Interrupt pins 18,19 on the mega. Using quadrature I get the speed of the motor. But when I attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE); with pins 2,3,20,or 21 instead of 18,19 nothing works anymore. Any clues? Do I have to treat those pins differently?

Thanks

Any clues?

No. It's impossible without seeing your code.

Do I have to treat those pins differently?

No and your attachInterrupt() appears fine since you call digitalPinToInterrupt() to get the correct vector.

Hey thanks for the answer,

your right sorry about the missing code. The isr gets called on pin 20 and 21. So nothing wrong with interrupt. But my routine is doing something wrong, I use the following for the encoder:

void count1()//ISR
{
  //set=true;
  static int8_t lookup_table[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  static uint8_t enc_val1 = 0;
  enc_val1 = enc_val1 << 2;
  enc_val1 = enc_val1 | ((PIND & 0b1100) >> 2);
  enc_count1 = enc_count1 + lookup_table[enc_val1 & 0b1111];
  
}

So the problem could be the PIND&0b1100 line. As it works with pin 18,19 with is portD, so I need to change it for 20,21. Any Idea how?

okay I got it to work. 20 and 21 is on Port D so the line will change to ((PIND & 0b0011));
as it is the 3rd and 4th bit of the PortD read byte. And then the shift of 2 is not needed anymore.

Posting code fragments will might get you guesses, not answers. You cannot test compile or test fragments.

Here’s my guess.

void count1()//ISR
{
  //set=true;
  static int8_t lookup_table[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  static uint8_t enc_val1 = 0;
  enc_val1 = enc_val1 << 2;
  enc_val1 = enc_val1 | (PIND & 0b11) ;
  enc_count1 = enc_count1 + lookup_table[enc_val1 & 0b1111];
  
}

You’ll find the pinout documents the easy way to find port mapping:

https://arduino-info.wikispaces.com/file/view/Mega2-900.jpg/421499040/Mega2-900.jpg