PinChangeInt freeze source code

Hi, I am new to this forum. Sorry for my bad english.. :frowning:

I using the arduino library PinChangeInt.h because I need 2 external interrupts in the pins A2 and A1. So, something is wrong is my code.. When I active the interrupt on pin A1, my code is freezing.. I start the program, I forced a interrupt in A2 (it's ok), when a forced interrupt on pin A1, my code is freeze..

Help..? My code:

#include "PinChangeInt.h"

bool bisr1 = false;
bool bisr2 = false;

void ISR_SWSERIAL_RX()
{
	bisr1 = true;
}

void ISR_SWSERIAL2_RX()
{
	bisr2 = true;
}

void setup()
{
  pinMode(A2, INPUT);
  pinMode(A1, INPUT);

  delay(500);

  Serial.begin(115200);

  attachPinChangeInterrupt(A2, ISR_SWSERIAL_RX, FALLING);
  attachPinChangeInterrupt(A1, ISR_SWSERIAL2_RX, FALLING);
}

void loop()
{
  	  if(bisr1)
      {
  		bisr1 = false;
  		Serial.println("-- OK 1--");

      }

  	  if(bisr2)
	  {
  		bisr2 = false;
		Serial.println("-- OK 2--");
	  }
}

You don't "attach" pin change interrupts, just write the ISR. Pin change is not edge-precise, not high priority.

Pin change interrupts

There are two ways you can detect external events on pins. The first is the special "external interrupt" pins, D2 and D3. These general discrete interrupt events, one per pin. You can get to those by using attachInterrupt for each pin. You can specify a rising, falling, changing or low-level condition for the interrupt.

However there are also "pin change" interrupts for all pins (on the Atmega328, not necessarily all pins on other processors). These act on groups of pins (D0 to D7, D8 to D13, and A0 to A5). They are also lower priority than the external event interrupts. You could make an interrupt handler to handle changes on pins D8 to D13 like this:

ISR (PCINT0_vect)
{
// one of pins D8 to D13 has changed
}

Clearly extra code is needed to work out exactly which pin changed (eg. comparing it to a "before" value).

Pin change interrupts each have an associated "mask" byte in the processor, so you could actually configure them to only react to (say) D8, D10 and D12, rather than a change to D8 to D13. However you still then need to work out which of those changed.

You can find more on the subject and more examples on the Arduino main site and on the web. What does not seem clear on one should become more clear from others and practice modifying examples. You can do this!

You don't "attach" pin change interrupts

#include "PinChangeInt.h"

That depends on the library syntax.

Which specific library are you using? Where did you get if from?

I recommend the one by Nico Hood, available through the library manager. It does use the syntax of external interrupts.

Go to the link I provided. Nick used no library, just IDE supported C/C++. It's not astrophysics.

cattledog:
Which specific library are you using? Where did you get if from?

I'm using: GitHub - GreyGnome/PinChangeInt: Pin Change Interrupt library for the Arduino

if you get a example code: PinChangeIntExample328.ino and comment the line ( attachPinChangeInterrupt(MYPIN3, pin3func, CHA... ):

...

// Attach the interrupts in setup()
void setup() {
  pinMode(MYPIN1, INPUT_PULLUP);
  attachPinChangeInterrupt(MYPIN1, quicfunc0, RISING);
  pinMode(MYPIN2, INPUT_PULLUP);
  attachPinChangeInterrupt(MYPIN2, quicfunc1, RISING);
  pinMode(MYPIN3, INPUT_PULLUP);
  //attachPinChangeInterrupt(MYPIN3, pin3func, CHANGE); // Any state change will trigger the interrupt.
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

...

I get the same problem. The program freeze and not interrupts more..

I'm using: GitHub - GreyGnome/PinChangeInt: Pin Change Interrupt library for the Arduino

NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
This library is deprecated as of April 3, 2015. This means that I will be
providing bug fixes for some time, but users are encouraged to migrate to
the EnableInterrupt library, at GitHub - GreyGnome/EnableInterrupt: New Arduino interrupt library, designed for Arduino Uno/Mega 2560/Leonardo/Due .
It is faster and easier to use. Thank you.

I used the EnableInterrupt library with your code as I don't have the older library installed. I used buttons on A1 and A2 with INPUT_PULLUP. All works as expected. How are you generating the interrupts on A1/A2?

How is software serial involved? What exactly are you trying to do?

#include <EnableInterrupt.h>
//#include "PinChangeInt.h"

bool bisr1 = false;
bool bisr2 = false;

void ISR_SWSERIAL_RX()
{
  bisr1 = true;
}

void ISR_SWSERIAL2_RX()
{
  bisr2 = true;
}

void setup()
{
  pinMode(A2, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);

  delay(500);

  Serial.begin(115200);

  attachPinChangeInterrupt(A1, ISR_SWSERIAL_RX, FALLING);
  attachPinChangeInterrupt(A2, ISR_SWSERIAL2_RX, FALLING);
}

void loop()
{
  if (bisr1)
  {
    bisr1 = false;
    Serial.println("-- OK 1--");

  }

  if (bisr2)
  {
    bisr2 = false;
    Serial.println("-- OK 2--");
  }
}

cattledog:
How is software serial involved? What exactly are you trying to do?

Thank you all for the answers!

Let me explain a little about the project.

I have a client who has a product developed by me, which in short performs an automation with four inputs and four outputs and sends the result through the arduino main serial. Now on this board, it will add to these four inputs, four barcode readers, which send the code through the 9600 serial protocol. For this reason, I started testing first for external interrupts, since I only have input pins A0, A1, A2 and A3.

Anyway, I've validated this EnableInterrupt library and it actually works, thanks. I believe the one I was using was obsolete and therefore should have some incompatibilities.

Rodrigo