Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created June 7, 2018 08:40
Show Gist options
  • Save mahenzon/f8b7e93dba716c861aa886afe5a6f9c5 to your computer and use it in GitHub Desktop.
Save mahenzon/f8b7e93dba716c861aa886afe5a6f9c5 to your computer and use it in GitHub Desktop.
Read Arduino serial port and print the line
String readString;
int state = LOW;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Starting");
}
void loop() {
while (Serial.available()) {
if (state == LOW) {
state = HIGH;
digitalWrite(LED_BUILTIN, HIGH);
}
delay(2); // wait to allow buffer to fill
if (Serial.available() > 0) {
char c = Serial.read(); // gets one byte from serial buffer
readString += c; // makes the str readString
}
}
if (readString.length() > 0) {
Serial.println(readString); // see what was received
readString="";
if (state == HIGH) {
state = LOW;
digitalWrite(LED_BUILTIN, LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment