Ok lets go back to basic ![]()
String inputString = ""; // 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:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
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;
}
}
}
You see, there is no need to take care of the interrupt. Its all internal stuff. You open the serial port, when stuff come in, the interrupt is triggered and the routine call serialEvent() all by it self.
This is useless
extern void serialEvent(void) __attribute__((weak));
As I stated up there, it is probable that if you try to catch the interruption by your self, you will interfere with the all ready made routine. The serialEvent() event will be call as soon that the Loop() routine is done. Do not worry, you will catch what has come in since there is a buffer to contain the in coming data.
And I wish to point out this part of the example:
/*
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.
*/
So learning to Blink without delay() could be a good thing