Clear existing array when getting new serial command

I’m starting to build my first Arduino project but I’m running into some problems with serial communication.

I get serial data from the console and store it in a char array called “nilai”.

Then, when I send a new console message to the Arduino, I want it to clear the existing “nilai” array and store only the new data in that array

this is the code

const int NUMBER_OF_FIELDS = 4; 
int fieldIndex = 0;             
int nilai[NUMBER_OF_FIELDS];

const int led1 = PB8;
const int led2 = PA5;
const int led3 = PA6;
const int led4 = PA7;

int gas = PC0;


// threshold value
//int nilai[4]={600,700,800,900};

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(gas, INPUT);

  digitalWrite(led1,HIGH);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,HIGH);

  Serial.begin(9600);
}

void loop() {
  
 if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value if the fieldIndex is within range
      // additional fields are not stored
      if(fieldIndex < NUMBER_OF_FIELDS) {
        nilai[fieldIndex] = (nilai[fieldIndex] * 10) + (ch - '0'); 
      }
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
        fieldIndex++;   // increment field index 
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      // in this example it's the newline character sent by the Serial Monitor
      
      // print each of the stored fields
      for(int i=0; i < min(NUMBER_OF_FIELDS, fieldIndex+1); i++)
      {
        Serial.println(nilai[i]);
       // nilai[i] = 0; // set the values to zero, ready for the next message
      }
      fieldIndex = 0;  // ready to start over
      
    }
  }

you can clear the array data by simply setting the fieldIndex to 0, but you already did that.

So that is the problem you are trying to solve?

arduino_new: you can clear the array data by simply setting the fieldIndex to 0, but you already did that.

So that is the problem you are trying to solve?

yes right, when I enter the new value its value becomes double my example input value is 400,500,600,700 read data is correct 400 500 600 700 but when I enter a new value with a value of 500,600,700,800 the data that was participated in lag with new data, the value read 400500 500600 600700 700800 and that's my problem. thanks

You basically can’t. The reason is that nilai contains binary data so any value is (in theory) valid. You also don’t have a need for it.

If you store text, e.g. the character ‘\0’ could be used to indicate that the data in the array is not valid. But you don’t use text (which is perfectly acceptable).

If you want to fill the array with predefined values, you can loop through the array and set everything. E.g.

   for (unsinged int cnt = 0; cnt < sizeof(nilai) / sizeof(nilai[0]);cnt++)
    {
      nilai[cnt] = 0xFFFF;
    }

You can replace 0xFFFF by any value that you want. The trick is that you choose a value that is not valid in your scenario (e.g. your numbers will only be between 0 and 999 or only positive); this can be used as a form of ‘clear’. If you don’t have a value that is invalid, you can’t ‘clear’ the array.

An alternative can be the use of memset

   memset(nilai, 0xFF, sizeof(nilai));

which in this case does the same as the approach with the for-loop.

// Added
You posted a reply indicating what goes wrong while I was composing this. In your case clearing the array means setting everything to 0 again; easiest is the memset approach and use 0 instead of 0xFF.

sterretje:
You basically can’t. The reason is that nilai contains binary data so any value is (in theory) valid. You also don’t have a need for it.

If you store text, e.g. the character ‘\0’ could be used to indicate that the data in the array is not valid. But you don’t use text (which is perfectly acceptable).

If you want to fill the array with predefined values, you can loop through the array and set everything. E.g.

   for (unsinged int cnt = 0; cnt < sizeof(nilai) / sizeof(nilai[0]);cnt++)

{
      nilai[cnt] = 0xFFFF;
    }



You can replace 0xFFFF by any value that you want. The trick is that you choose a value that is not valid in your scenario (e.g. your numbers will only be between 0 and 999 or only positive); this can be used as a form of 'clear'. If you don't have a value that is invalid, you can't 'clear' the array.

An alternative can be the use of memset


memset(nilai, 0xFF, sizeof(nilai));



which in this case does the same as the approach with the for-loop.

// Added
You posted a reply indicating what goes wrong while I was composing this. In your case clearing the array means setting everything to 0 again; easiest is the memset approach and use 0 instead of 0xFF.

i’ve tried using both its source but still fail. if i use

memset (value, 0xFF, sizeof (value));

I try to input the value 400,500,600,700
value data unchanged
value in reading
400
-1
-1
-1
but if I use this code

for (unsigned int cnt = 0; cnt <sizeof (value) / sizeof (value [0]); cnt ++)
    {
      value [cnt] = 0xFFFF;
    }

then the value in the read
65535
65535
65535
65535

const int NUMBER_OF_FIELDS = 4; 
int fieldIndex = 0;             
int nilai[NUMBER_OF_FIELDS];

const int led1 = PB8;
const int led2 = PA5;
const int led3 = PA6;
const int led4 = PA7;

int gas = PC0;


// threshold value
//int nilai[4]={600,700,800,900};

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(gas, INPUT);

  digitalWrite(led1,HIGH);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,HIGH);

  Serial.begin(9600);
}

void loop() {
  
  
 if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value if the fieldIndex is within range
      // additional fields are not stored
      if(fieldIndex < NUMBER_OF_FIELDS) {
        nilai[fieldIndex] = (nilai[fieldIndex] * 10) + (ch - '0'); 
      }
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
        fieldIndex++;   // increment field index 
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      // in this example it's the newline character sent by the Serial Monitor
      
     // print each of the stored fields
     // for(int i=0; i < min(NUMBER_OF_FIELDS, fieldIndex+1); i++)
     // {
     //   Serial.println(nilai[i]);
     //   nilai[i] = (char)0; // set the values to zero, ready for the next message
     //   memset(nilai, 0xFF, sizeof(nilai));  // test
     // }
     for (unsigned int cnt = 0; cnt < sizeof(nilai) / sizeof(nilai[0]);cnt++) //test
    {
      nilai[cnt] = 0xFFFF;
      Serial.println(nilai[cnt]);
    }
      
      fieldIndex = 0;  // ready to start over
    }
  }

  
  int analogSensor = analogRead(gas);
  Serial.print("Nilai Sensor: ");
  Serial.println(analogSensor);

  if ((analogSensor > nilai[0]) && (analogSensor < nilai[1])) 
    {
    digitalWrite(led1, LOW);}
    else{digitalWrite(led1,HIGH);
    }
  if((analogSensor > nilai[1]) &&( analogSensor < nilai[2]))
    {digitalWrite(led2,LOW);}
    else{digitalWrite(led2,HIGH);
    }
  if((analogSensor > nilai[2]) &&( analogSensor < nilai[3]))
    {digitalWrite(led3,LOW);}
    else{digitalWrite(led3,HIGH);
    }
  if(analogSensor > nilai[3])
    {digitalWrite(led4,LOW);}
    else{digitalWrite(led4,HIGH);
    }
delay(500);
}

See my //Added comment at the end of the post; set it to zero.

You're global nilai array is initialised with zeros when the program starts running; that is the status that you want again when you receive a new set of data

Have a look at how it is done in Serial Input Basics - simple reliable ways to receive data.

...R