Created
December 24, 2015 23:47
-
-
Save RealOrangeOne/ce2ced6f3ecd69e4722b to your computer and use it in GitHub Desktop.
Arduino Multi-character serial reader with check for command input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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