Created
June 11, 2013 19:16
-
-
Save ladislas/5759763 to your computer and use it in GitHub Desktop.
an easy way to convert Serial.read() to string.
This file contains 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
// Buffer to store incoming commands from serial port | |
String inData; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Serial conection started, waiting for instructions..."); | |
} | |
void loop() { | |
while (Serial.available() > 0) | |
{ | |
char recieved = Serial.read(); | |
inData += recieved; | |
// Process message when new line character is recieved | |
if (recieved == '\n') | |
{ | |
Serial.print("Arduino Received: "); | |
Serial.print(inData); | |
// You can put some if and else here to process the message juste like that: | |
if(inData == "+++\n"){ // DON'T forget to add "\n" at the end of the string. | |
Serial.println("OK. Press h for help."); | |
} | |
inData = ""; // Clear recieved buffer | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment