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);
}