Skip to content

Instantly share code, notes, and snippets.

@ladislas
Created June 11, 2013 19:16
Show Gist options
  • Save ladislas/5759763 to your computer and use it in GitHub Desktop.
Save ladislas/5759763 to your computer and use it in GitHub Desktop.
an easy way to convert Serial.read() to string.
// 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