Hi
I am trying to scan a string using the code below. I can scan the integers, but not the strings. Could you please give a hint? I defined the string "day" in the beginning, and I am trying to scan the code by adding values to serial monitor. My input values in serial monitor is with the following format:
12,Monday
I have noticed that when I add 2-3 characters, the returned value in serial monitor is empty, and when I add 6-7 characters (let's say Monday), there is a huge amount of rubish text appearing in the serial monitor.
// Example 2 - Receive with an end-marker
#include <String.h>
int hour = 0;
int minute ;
String day ;
String str = "A String";
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) {
sscanf(receivedChars, "%d,%s", &hour,&day);
Serial.print("This just in ... ");
Serial.println(hour);
Serial.println(day);
Serial.println(str);
newData = false;
}
}