Interrupts on pins 20, 21 blocked on MEGA while using I2C

Hello,
first I want to apologise that I wrote a lot of text but this should help you help me solve the problem :slight_smile: of course if it is solvable. It is kinda simply complicated :astonished: Please do read if u have experience cuz this problem is confusing me like hell.
I'm using an Arduino MEGA 2560 and on top of it two motor control boards, namely the "Adafruit motor shields v2". These two drivers are connected with the MEGA board of course using I2C. The two drivers are mounted on top of the Arduino board and the pins 20 and 21 are free and not connected.
Now I want to use the INTERRUPTs 2,3 on the MEGA which are pins 20 and 21.
My program does not start after uploading it (or hitting the reset button) IF BOTH of the following are done:
1- The use of the I2C protocol is declared in the Setup function (in order to initiate the motor drivers)
2- The encoder wires are connected to pins 20 or 21.

As far as I know, those are the I2C pins on the MEGA board and the communication with the drivers could probably be causing the problem. Is there any way that I could still use the Adafruit shields and taking interrupts from the above mentioned pins?

Note 1 (Update): First I tried getting the interrupts simply with a manual code in my program. I have the motor drivers ON and working fine. All other interrupts work fine as well. Though when I start the program with the encoder wires inside the 20 and 21 pins it doesn't start and the serial monitor doesn't show anything. Now if I disconnect the encoder wires on 20 and 21, restart the program and then reconnect them, the program seems to work! I still couldn't get any data from pins 20 and 21 though.
Now I got a new source of encoder code. I installed the Arduino encoder library, <encoder.h>. The problem continues when the program starts with the encoder wires in the pins. Though if I connect the wires after starting the program, it works and I GET INTERRUPTS FORM 20 AND 21!!!!!! :fearful: :fearful: :fearful:
It just confused me more! Sometimes it even starts to work if I start the program with the wires is and simply draw them out and put them back in again! I don't even need to restart the program! ( but just sometimes).

Note 2: Sometimes when I start the program with the motor connected to the encoder running (meaning the encoder signal is varying) it starts and works normally.

@em_muh:

I do not have your answer, but to get this in front of forum members that may have an idea, you need to comply with posting your complete code in code-tags. All of this is explained in the forum "stickies" at the top of the "Microcontrollers" sub-forum.

Also, links to the product and to the library. If you have a pix or two of your prototype this will be helpful, too, as it may suggest to some of the gurus additional dialog with you.

Oh, and welcome to the forum!

Ray

Update:
It seems to be unsolvable!
Even when it works (when the encoder shows results), the I2C doesn't seem to function! So if the code requires I2C connection, one of the two functions wouldn't work. Either I2C or interrupt! And since the interrupts always have the advantage they wouldn't let I2C work if they occur on those pins.
If anybody has another opinion please share!

One of the better things about buying from Adafruit is that a portion of your cost goes into their support forum... have you tried to ask there?
http://forums.adafruit.com/

Ray

Of course it wont... INT2 and INT3 are pins 19 and 18 respectively...

Furthermore if you are using I2C you cant use INT0 and INT1... because they are they are physically the same pins as I2C.

mrburnette:
@em_muh:
you need to comply with posting your complete code in code-tags.
...............
Oh, and welcome to the forum!

I think in my case the code is kinda irreverent. It is about the structure of the microcontroller.
..Thank you :slight_smile:

INT0 and INT1 on the MEGA are Pins 2,3. INT2 and INT3 are pins 21 and 20 respectively! I2C are 20 and 21 as well. I think you confused it with another Arduino :wink:

mrburnette:
One of the better things about buying from Adafruit is that a portion of your cost goes into their support forum... have you tried to ask there?

I already had done it. No solution from their point of view. They have very good support BTW, although they had forgotten to mention a very important thing in the specs of the board that I'm using and I had to tell them about it after being stunned after buying the product without knowing :stuck_out_tongue: They did write it in the specs after the incident though! haha :smiley:

they had forgotten to mention a very important thing in the specs of the board that I'm using and I had to tell them about it after being stunned after buying the product without knowing smiley-razz They did write it in the specs after the incident though!

If the specs were wrong, you can request a refund through the support channel.

I think in my case the code is kinda irreverent. It is about the structure of the microcontroller.

It's a rule, sooner or later you will run afoul of one of the moderators. It is considered a courtesy to avoid wasted time... Forums are very poor chat rooms. But, I understand your argument... But, I don't much matter, either. :fearful:

Good luck,

Ray

The two drivers are mounted on top of the Arduino board and the pins 20 and 21 are free and not connected.

Pins 20 and 21 ARE the I2C pins of the MEGA, so when you are using them for I2C, you can NOT also use them as interrupt pins.
If you look at the schematic/PCB for the MEGA, you'll find that the the "R3" I2C pins ARE connected to 20/21 on the MEGA board itself:

em_muh:
INT0 and INT1 on the MEGA are Pins 2,3. INT2 and INT3 are pins 21 and 20 respectively! I2C are 20 and 21 as well. I think you confused it with another Arduino :wink:

Nope. INT4 and INT5 are pins 2 and 3. Its just that the folks at Arduino in their infinite wisdom decided to name them differently from what ATMEL calls them just to be awkward.

    case 2:
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
      EIMSK |= (1 << INT0);
      break;
    case 3:
      EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
      EIMSK |= (1 << INT1);
      break;
    case 4:
      EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
      EIMSK |= (1 << INT2);
      break;
    case 5:
      EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
      EIMSK |= (1 << INT3);
      break;
    case 0:
      EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
      EIMSK |= (1 << INT4);
      break;
    case 1:
      EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
      EIMSK |= (1 << INT5);
      break;
    case 6:
      EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
      EIMSK |= (1 << INT6);
      break;
    case 7:
      EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
      EIMSK |= (1 << INT7);
      break;

So based on that, INT2 and INT3 (pins 19 and 18) are named in Arduino as EXTERNAL_INTERRUPT_4 and EXTERNAL_INTERRUPT_5.

EDIT:

And a table:

REAL Interrupt Number Arduino Interrupt Number Arduino Pin Number
INT0 EXTERNAL_INT_2 D21
INT1 EXTERNAL_INT_3 D20
INT2 EXTERNAL_INT_4 D19
INT3 EXTERNAL_INT_5 D18
INT4 EXTERNAL_INT_0 D2
INT5 EXTERNAL_INT_1 D3
INT6 EXTERNAL_INT_6 No Pin
INT7 EXTERNAL_INT_7 No Pin

To make life easier I wish people would call them by either the real name (INTx meaning ATMELs naming) or their Arduino name (EXTERNAL_INT_x meaning Arduino's naming).

I didn't know that. I never used ATMEL before. Thanks :slight_smile:

So I guess they also denied us the use of the EXTERNAL_INT_6 and EXTERNAL_INT_7!!!!
So if one uses IĀ²C he loses 4 interrupt possibilities automatically! This is kinda unfair :expressionless:

I2C only costs you two interrupts, the other two are simply not wired up to any pins.

p.s. "ATMEL" is the company that makes the ATMega chips, so byte "what ATMEL calls them" I am referring to what they are called in the datasheet for the chip.

Yes I got it. I'm familiar with the ATMega Microcontrollers although I never used any of them. I meant ATMega instead of ATMEL. Sorry :stuck_out_tongue: