serialEvent() incompatible with attachInterrupt

Wish you all a good day!
As a newbie to arduino I need your help.

I need to get some data from serial port, just like in SerialEvent tutorial. That is fine.
But when I add any (even empty) function to handle interrupts and an attachInterrupt() to my Setup section, serialEvent seems to be never called anymore.
I put here a part of my code, which is complete working sketch: it works fine with serial port, while attachInterrupt string is commented/removed, and stops when I uncomment it.
Any help will be appreciated.

const int boxid = 1; //Number, or ID of the box. Each box has own ID


const int intpin = 2;     //4 Hz interrupt

volatile int leftvalue = 0; //read from A0
volatile int rightvalue = 0; //read from A1
volatile int ratpos = 0; //0 -  in the left box, 1 - in transition to right box, 3 - in the right box

String ratid; //rat ID

int c = 0; //time counter for delays

String istr = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  istr.reserve(200);
  ratid.reserve(4);
  attachInterrupt(digitalPinToInterrupt(intpin), checkrat, LOW);
}

void loop() {
  // print the string when a newline arrives:
 if (stringComplete)
  {
      Serial.println("got string: ");
      Serial.println(istr);
      parsestring();
      Serial.println("got ratID: ");
      Serial.println(ratid);
      Serial.println('\n');
      stringComplete = false;
  };
}

void checkrat()
{
  
}//checkrat


void serialEvent()
{
  while (Serial.available()) 
  {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    istr = istr + inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') 
    {
      stringComplete = true;
      
    }
  }
}

void parsestring()
{
  String temp ="";
  Serial.println(istr.substring(0,3));
  if (istr.substring(0,3) == "box")
   {
     temp = istr[4];
     if (temp.toInt() == boxid)
      {
        ratid = istr.substring(9);
      } //temp.toint == boxid
      } // if istr begins with 'box'
}//parsestring

Check the condition present on intpin

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Because your ISR is called when the pin is LOW (rather than when it is FALLING) it is probably called so often that nothing else gets a look-in.

You need to explain what the ISR is intended for.

The serialEvent() function is exactly equivalent to

if (Serial.available() > 0) {
   mySerialFunction();
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

…R
Serial Input Basics

Well, for one thing, doing attachInterrupt(pin, LOW) will generate a constinuous interrupt whenever the pin is LOW, leaving no time for the processor to do anything else. Use RISING, FALLING, or CHANGE instead of LOW.

Regards,
Ray L.

This ISR is driven by pin2, which is, in turn, connected to a source of square 4 Hz impulses. Checkrat() function reads 2 analog values from photocouples and matches them against predefined threshold, thus determining, in which part of box the rat currently is. Everything except the communication code (which I added today) worked fine.
I'll check, perhaps, the wire came off :wink:

I'll check, perhaps, the wire came off

Worse, it might be connected.
See reply #3

Thanks all! You've helped a lot!
The problem was almost hardware :slight_smile:
As Arduino starts while connected just to programmer, and I didnt need all the functions of the entire setup, I didnt switch power on. So the interrupt generator was continuously low and interrupt really took all the time.
Not all, exactly, but about 99% of it. The rest was enough to send protocol initiation messages to serial port every 2 seconds, but incoming data was lost due to timeouts.