Skip to content

Instantly share code, notes, and snippets.

@HouzuoGuo
Last active May 16, 2021 08:48
Show Gist options
  • Save HouzuoGuo/90cea01dbe2c7b6d7815de243a33e1b2 to your computer and use it in GitHub Desktop.
Save HouzuoGuo/90cea01dbe2c7b6d7815de243a33e1b2 to your computer and use it in GitHub Desktop.
Arduino UNO R3 serial port example: read input string via serial and print it back
void setup()
{
Serial.begin(1200);
Serial.setTimeout(1000);
Serial.println("Microcontroller has started");
}
int seqNum = 0;
String lastRead = "";
void loop()
{
char log[128];
sprintf(log, "%d going to read a string using time out of 1000ms", seqNum++);
Serial.println(log);
if (Serial.peek() == -1)
{
sprintf(log, "%d received nothing before timing out", seqNum++);
delay(1000);
Serial.println(log);
}
else
{
lastRead = Serial.readString();
}
sprintf(log, "%d last read string is: %s", seqNum++, lastRead.c_str());
Serial.println(log);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment