Skip to content

Instantly share code, notes, and snippets.

@RealOrangeOne
Created December 24, 2015 23:47
Show Gist options
  • Save RealOrangeOne/ce2ced6f3ecd69e4722b to your computer and use it in GitHub Desktop.
Save RealOrangeOne/ce2ced6f3ecd69e4722b to your computer and use it in GitHub Desktop.
Arduino Multi-character serial reader with check for command input
const char endChar = '\n';
const char commandIdent = '$';
String inputString;
boolean hasInput = false;
boolean isCommand = false;
void setup() {
Serial.begin(9600);
}
void loop() {
if (hasInput) {
if (isCommand) {
Serial.println("Got command: " + inputString);
} else {
Serial.println("Got input: " + inputString);
}
inputString = "";
isCommand = false;
hasInput = false;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == endChar) { // It's the end of the input stream, let's work out what it is
hasInput = true;
if (inputString.startsWith(String(commandIdent))) {
isCommand = true;
inputString.remove(0, 1);
}
} else {
inputString += inChar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment