// https://wokwi.com/projects/409958888939313153
// Code from https://forum.arduino.cc/t/serial-input-basics-updated/382007#example-2-receiving-several-characters-from-the-serial-monitor-7
// Other simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754
/*
Example 2 - Receiving several characters from the Serial Monitor
If you need to receive more than a single character from
the Serial Monitor (perhaps you want to input people's names) you
will need some method of letting the Arduino know when it has
received the full message. The simplest way to do this is to set
the line-ending to newline.
This is done with the box at the bottom of the Serial Monitor
window. You can choose between "No line ending", "Newline",
"Carriage return" and "Both NL and CR". When you select the
"Newline" option a new-line character ('\n') is added at the
end of everything you send.
*/
// Example 2 - Receive with an end-marker
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}