Array not empty

This code receives a message that mas a max lenght of 13 characters. This message is stored in strValue. After receiving the message it must be cleared. Placing a NULL character at strVal[0] schould do the job but it doesnt.

#include <LiquidCrystal.h>
#include <HCPCA9685.h>

#define BAUDRATE  9600
#define LCD_ROWS  2
#define LCD_COLS  16
#define I2C_ADR   0x40
#define VERSION   "XtrackTerm V012z"

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//HCPCA9685 HCPCA9685(I2C_ADR);

void setup() {
  Serial.begin(BAUDRATE);
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.clear();
  lcd.print(VERSION);
  delay(2000);
  lcd.clear();
  /* Wake the device up */
  //  HCPCA9685.Init(SERVO_MODE);
  //  HCPCA9685.Sleep(false);
  lcd.setCursor(0, 0);
  //         1234567890123456
  lcd.print("Waiting for data");
  Serial.println(">>initialized");
}
// Where receiving a string like 123.45,123.67\n
const int MAX_LEN = 14; // the max digits in the number plus decimal point
char  strInput[MAX_LEN + 1]; // add one for the terminating null
char  *strXval;    // ASCII X axis
char  *strYval;    // ASCII Y axis
int str_Index = 0; // conter for receiving string characters

void loop() {
  //Check to see if anything is available in the serial receive buffer
  while (Serial.available() > 0)
  {
    static char strValue[MAX_LEN];  //  hold the incoming message
    char inByte = Serial.read();  //Read the next available byte
    //Message coming in (check not terminating character) and guard for over message size
    if ( inByte != '\n' && (str_Index < MAX_LEN - 1) )
    {
      lcd.setCursor(0, 0);
      //         1234567890123456
      lcd.print(" Receiving data ");
      //Add the incoming byte to our message
      strValue[str_Index++] = inByte;
      Serial.println(strValue);
    } // end if
    //Full message received...
    else
    {
      //Add null character to string
      strValue[str_Index] = NULL;
      Serial.println("process data");
      strValue[0] = NULL;  // Clear the array
      str_Index = 0; // reset array index
      Serial.println(strValue);
    } // end else
  } // end while
} // end loop

Any suggestions, Harry

Please explain what is expected to happen and what is happening

if I enter 1234567890123 for the first time the output looks like this:

[code]>>initialized
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
process data
[\code]This is correct, but when I send the second message (32310987654321) I see this:[code]
3234567890123
3234567890123
3214567890123
3210567890123
3210967890123
3210987890123
3210987890123
3210987690123
3210987650123
3210987654123
3210987654323
3210987654323
3210987654321
process data
[\code] where can be seen that the array is not empty.

Harry

Setting strValue[0] to 0 clears the string, but then you over-write the 0 with a single byte read from the serial, and the rest of strValue is still there - there is no more terminating null until the end of the previous "full message"

When reading strings byte-by-byte, you need to keep propagating the null (or you need to zero out the entire array.)

      strValue[str_Index++] = inByte;
      strValue[str_Index] =0;

you declared array static, are you using it elsewhere, besides the loop function?

No I only use it in the loop function, actualy only in the while loop

You could move it before while loop and make sure it is zeroed before you use it

…
if (Serial.available() > 0)
{
    char strValue[MAX_LEN] = {0};
    while (Serial.available() > 0)
    {
…