I have been trying to use the pinchangeint library and have been having quite a lot of trouble with it. I am unable to get most pins to work as interrupts.
#include <PinChangeInt.h> //Include interrupt library
void setup() {
Serial.begin(9600); //Start a serial connection
pinMode(53, INPUT); //make all the pins I am using inputs
pinMode(51, INPUT);
pinMode(49, INPUT);
pinMode(47, INPUT);
PCintPort::attachInterrupt(53, Function53, CHANGE); //declare function that will be called when pin states are changed
PCintPort::attachInterrupt(51, Function51, CHANGE);
PCintPort::attachInterrupt(49, Function49, CHANGE);
PCintPort::attachInterrupt(47, Function47, CHANGE);
}
void loop() {
while(true); //program must repeat this loop and only leave to perform interrupts
}
void Function53(){ //These are my four interrupts. very simple. Just print out what function is called.
Serial.println("Function 53");
}
void Function51(){
Serial.println("Function 51");
}
void Function49(){
Serial.println("Function 49");
}
void Function47(){
Serial.println("Function 47");
}
When I compile and run the code above. Interrupt 51 and 53 work perfectly, but not the other two.
I have tested my hardware and that is not the problem.
Does anyone have any experience with this library? Is there any limitations?
Would you mind terribly explaining what you meant with:
"Maybe you need to stop doing things that need interrupts enabled, in you interrupt service routines where interrupts are disabled."
The ATmega328P has PCINTs on every pin. Sadly, that is not the case for the ATmega2560.
This is the pinmapping : https://www.arduino.cc/en/Hacking/PinMapping2560
Click on the picture to see it better.
Pin 47 and 49 don't have a PCINT, pin 51 and 53 do (PCINT2 and PCINT0).
The analog inputs A6...A15 do all have PCINT, and a few other pins.
Would you mind terribly explaining what you meant with:
Not at all. When your interrupt service routine (Function53()) is called, interrupts are disabled. Serial.print() writes data to a buffer, and returns, IF there is room in the buffer.
If there is not, Serial.print() waits for room to be made in the buffer, then it writes to the buffer, and returns.
How does room get made in the buffer? Well, the data that is already in the buffer needs to be removed. That happens when the appropriate interrupt service routine is triggered, to send a byte out.
Since interrupts don't happen, while your interrupt service routine is running, if there is not room in the buffer, for Serial.print() to write to, it will block, waiting for room to be made, which can not happen, since the mechanism for making room is disabled.