Skip to content

Instantly share code, notes, and snippets.

@edy555
Last active December 17, 2015 08:49
Show Gist options
  • Select an option

  • Save edy555/5582797 to your computer and use it in GitHub Desktop.

Select an option

Save edy555/5582797 to your computer and use it in GitHub Desktop.
Work around for USB CDC based Arduino. Arduino 'Serial Event' example does not work on USB CDC based arduino such as Leonard or DaVinci board.
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
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_event() {
if (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
void loop() {
loop_event();
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment