Reading Serial strings with SerialEvent - Arduino locks after 2-3 lines

Hi,

I'm trying to use SerialEvent to read arbitrary lines of ASCII text from serial input. It looks for a newline and then sets the string as complete, just like in the example for the SerialEvent function. I'm just using the Serial Monitor in the IDE to communicate at this point. After 2 lines (or sometimes 3, if they're short) the board stops reading new serial input. I added LED-blinking code to the program for troubleshooting purposes-- it actually stops blinking after reading that 2nd or 3rd line. So it seems to be locking up the board.

I am baffled. Does anyone have any ideas?

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

int led = 13;  // Will blink for troubleshooting


void setup(){
  Serial.begin(9600);
  inputString.reserve(200);
  
  pinMode(led, OUTPUT);  // Will blink for troubleshooting
}

void loop() {
  
  //check if serialEvent received a newline and send that to my printRegular function.  
  if (stringComplete) {
    printRegular(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
  
  //Blink!
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += 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;
    } 
  }
}

String printRegular(String s) {
  // I want to do other stuff here, but I'm just doing Serial.println for debug
        Serial.println(s);
}

Does anyone have any ideas?

Sure. Use strings, not Strings. All that dynamic memory allocation on an Arduino with little memory is not good.

Not familiar with the serial event thing, but you might try something similar to the below to delimit your data.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //if (readString.length() >0) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString
        if(readString.indexOf("on") >=0)
        {
          digitalWrite(ledPin, HIGH);
          Serial.println("LED ON");
        }
        if(readString.indexOf("off") >=0)
        {
          digitalWrite(ledPin, LOW);
          Serial.println("LED OFF");
        }       
        readString=""; //clears variable for new input
      //}
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}