Clearing inputstring provides strange squares

I hope I posted this item all right, I got the advise to read "how to post a question":frowning: . I use the Serial.read statement to read in a string. When a comma is read, the program should clear the input string.

I store the input in an array STRING []. When I print the strings, the first one dispays OK, the others are preceded by an "open square", see attachment.

Question: What is happening and how can I avoid this. This makes it impossible to check the strings on further development.

Below my sourcecode:

int stringTel = 0;
String rx_str;
char rx_byte;
String rx_str_old = "";
String inputStrings[5]= {"een","twee","drie","vier"};

void setup(){
Serial.begin(115200);
Serial.println("readInput_20191003");

Serial.println("printen intitwaarde");
for (int localTel2 = 0; localTel2 < 3;localTel2++){
Serial.println(inputStrings[localTel2]);
}
}

void loop(){

input();

if (inputStrings[1] == "vijf"){
Serial.println("goed gezien");
Serial.println(inputStrings[1]);
}
/*
else{
Serial.println("fout gezien");
}
*/

}

void input(){

if (Serial.available() == 0){
return;
}

rx_str = "";
rx_byte = "";
stringTel = 0;
for (int localTel1 = 0;localTel1 < 5;localTel1++){
inputStrings[localTel1] = "";
}

while (Serial.available() > 0){
rx_byte = Serial.read();
delay(30);
if (rx_byte == ','){
Serial.println("komma gespot");
stringTel = stringTel + 1 ;
Serial.println(stringTel);
rx_byte= "";
rx_str = "";
Serial.print(rx_byte);
}

if (rx_byte != ""){
Serial.print(rx_byte);
rx_str += rx_byte;
inputStrings[stringTel] = inputStrings[stringTel] + rx_byte;
// Serial.print("array : ");
// Serial.println(stringTel);
// Serial.println(inputStrings[stringTel]);

}
}

Serial.println("einde invoer");

for (int localTel2 = 0; localTel2 < 3;localTel2++){
Serial.print("output: ");
Serial.println(localTel2);
Serial.println (inputStrings[localTel2]);
}

}

ArduinoForum20191012.JPG

I hope I posted this item all right, I got the advise to read "how to post a question"

Did you choose to ignore the advice to use code tags when posting your code or did you miss it ?

You have declared rx_byte as char and you use it as a String - thats all. I did some changes at your scetch, please compare. From my point of view it works.

int stringTel = 0;
String rx_str;
char rx_byte;              // This is a char
String rx_str_old = "";
String inputStrings[5] = {"een", "twee", "drie", "vier", "five" };  // Why only 4 initial values?

void setup() {
  Serial.begin(38400);
  Serial.println("readInput_20191003");

  Serial.println("printen intitwaarde");
  for (int localTel2 = 0; localTel2 < 3; localTel2++) {
    Serial.println(inputStrings[localTel2]);
  }
}

void loop() {

  input();

  if (inputStrings[1] == "vijf") {
    Serial.println("goed gezien");
    Serial.println(inputStrings[1]);
  }
  /*
    else{
    Serial.println("fout gezien");
    }
  */


}

void input() {

  if (Serial.available() == 0) {
    return;
  }

  rx_str = "";
  rx_byte = 0;
  stringTel = 0;
  for (int localTel1 = 0; localTel1 < 5; localTel1++) {
    inputStrings[localTel1] = "";
  }


  while (Serial.available() > 0) {
    rx_byte = Serial.read();
    delay(30);
    if (rx_byte == ',') 
    {
      Serial.println("komma gespot");
      stringTel = stringTel + 1 ;
      Serial.println(stringTel);
      rx_byte = 0;
      rx_str = "";
      Serial.print(rx_byte);
    }

    if (rx_byte != 0) {
      Serial.print(rx_byte);
      rx_str += String(rx_byte);  //--- The most important change 
      inputStrings[stringTel] = inputStrings[stringTel] + rx_byte;
      //      Serial.print("array :  ");
      //      Serial.println(stringTel);
      //      Serial.println(inputStrings[stringTel]);

    }
  }

  Serial.println("einde invoer");

  for (int localTel2 = 0; localTel2 < 3; localTel2++) {
    Serial.print("output:  ");
    Serial.println(localTel2);
    Serial.println (inputStrings[localTel2]);
  }

}

btw: It seems you have not read "How to use this forum..."

Hello RudolfAtRTC,

Thanx a bunchm learned something new again.

I did read the "How to use . . . " but I understand I still did something wrong. I'll read it again.

Thanks again,

Henk

good luck as you learn!
it’s fun