// code uses a nested if (Serial.available() > 0 ) { ... }
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Enter a string and press Enter:");
}
void loop() {
if (Serial.available() > 0) {
char ch = Serial.read();
String str = ""; // create an Arduino String object
while (ch != '\n') {
if (ch >= 32 && ch <= 126) { // check if the character is printable
str += ch; // append the character to the string
}
if (Serial.available() > 0) {
ch = Serial.read(); // read the next character
}
}
Serial.print("Received String: ");
Serial.println(str);
Serial.println("Enter another string and press Enter:");
}
}