Secret array

Am using an arduino Mega to automate a test process. A counter on a flowmeter increments until a specific volume passes. At various points until the full volume is counted the code below calls P1_process to read a value from a serial port and fill an array with 5 doubles.

   if (count > P1_trip_pts[P1_ind] && P1_request == false){
    P1_request = true; 
   }
   if (P1_request == true){
    P1_process(); 
   }

P1_process, displayed below, flushes the serial buffer, waits for a linefeed, accumulates a full string, reads it, and then converts to a double using atof, which is stored in array P1_flt[]

void P1_process(){  //read serial port 1 for P1********************
    while (Serial1.available() && P1_ready == false) {
      int chk_lf = Serial1.read(); 
      if (chk_lf == 10) {
       P1_ready = true;
      } 
    }
 
    if(P1_ready && Serial1.available() >15) {
     for (int i=0; i <= 15; i++){
      P1_str[i] = Serial1.read();
     }
     P1_flt[P1_ind] = atof ( P1_str );
     P1_done = true;
     P1_request = false;
     P1_ready = false;
     ++P1_ind;
   }
    
  return; 
 }        //end of P1_process**********************

When the test is completed, the P1_flt[] values are output:

    Serial.print("P1:,");
    for (int i=1; i <= 5; i++){
     Serial.print(P1_flt[i],3);
     Serial.print(",");
    }

and the array zeroed out for the next test:

      for (int i=0; i <= 5; i++){
       P1_flt[i] = 0.0;
      }

The problem is that when the array values are output, they are from a previous test, even though the P1_flt[] values appear to have been properly zeroed out between tests. I can't imagine where the old values are being stored, and how to fix this. help.
there is much more code (15k) which all seems to work ok, but I tried to provide the relevant portions to this problem.

Each of your loops is being executed when the index into P1_flt is 0, 1, 2, 3, 4, and 5. That's 6 times. How big is P1_flt?

From this data, we can't tell that the code to set P1_flt[n] to 0.0 is being called.

Finally, in P1_process, you do nothing until at least one byte is available in the serial buffer. As soon as that one byte is present, and is a line feed, you check to see if there are 16 or more bytes.

If not, you don't ready anything. Still, whatever was left over in P1_str is re-processed.

I think that P1_str is your secret array. It's been outed now. :wink:

When I dug into this it did trace back through P1_str. I had forgotten to use Serial1.flush() to clear the buffer before looking for new values. By happenstance, the buffer holds just enough characters to convert and fill my 5 double array. So it appeared that there was a 'secret array' of five doubles, but in reality it was just the unflushed serial buffer.
Thanks for sending me in the right direction. and for pointing out some other kludgy issues.
tnx
bcs

I had forgotten to use Serial1.flush() to clear the buffer before looking for new values.

Why would you flush the buffer before trying to read from it? The problem was not that you were using data stored in a string from a previous pass, if there was no data to replace the string contents on this pass.

The correct solution is to either initialize the string correctly, or not try to read the contents of the string if you haven't written new data to the string.

Well, here is the rationale. The pressure instruments connected to the serial ports send a reading every second, continuously. To get the current reading at a given time, I wanted to flush all of the accumulated characters out of the buffer, wait for a linefeed, and then wait for the latest value from the instrument to show up at the serial port. reasonable?

Yes, that is reasonable. As long as you understand that you are missing a lot of data, and as long as it doesn't matter.