Last active
May 16, 2021 08:48
-
-
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
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
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