Sprintf dont print some char, but anothers do It.

Hi to you all.

EXTRACT:
I have this program, witch sense 6 NTC and send the temperatures to a float array, then it uses the float value to convert it, to print on an LCD display.

And for the last use the value of the float sensor value to save it on a SD file.

Analog read ------ OK
Convert to float --OK
convert to char ---OK
print on LCD ------OK

Use the same float value as before to convert it to a char and print DONT OK;

I will upload the entire file (its messy is work in progress)
But this what it bothers me;

I send to serial print the values before convert them in to chars;

if( saveToSD == true) 
  { 
    Serial.println(fechaStr ); 
    Serial.print("1.-"); Serial.println(sensorTemp[0] ); 
    Serial.print("2.-"); Serial.println(sensorTemp[1] ); 
    Serial.print("3.-"); Serial.println(sensorTemp[2] ); 
    Serial.print("4.-"); Serial.println(sensorTemp[3] ); 
    Serial.print("5.-"); Serial.println(sensorTemp[4] ); 
    Serial.print("6.-"); Serial.println(sensorTemp[5] );

And it prints ok;

1.-18.62
2.-18.25
3.-18.25
4.-18.62
5.-18.25
6.-18.62

Yet if I use the code below; Yields to an error

dtostrf(sensorTemp[0], 5, 1, CT1 );
dtostrf(sensorTemp[1], 5, 1, CT2 );
dtostrf(sensorTemp[2], 5, 1, CT3 );
dtostrf(sensorTemp[3], 5, 1, CT4 );
dtostrf(sensorTemp[4], 5, 1, CT5 );
dtostrf(sensorTemp[5], 5, 1, CT6 );

sprintf(line1_data, " F%s H%s 1%s 2%s 3%s 4%s 5%s 6%s", fechaStr, horaStr, CT1, CT2, CT3, CT4, CT5, CT6);

F21/03/2019 H10:53:24 1!
!* 2 3 4 18.6 5 18.3 6 18.6

This what I should have instead:

F21/03/2019 H10:53:24 1 18.7 2 18.2 3 18.7 4 18.6 5 18.3 6 18.6

So, for some reason the code is correctly reading and converting the last 3 floats but no the first 3
I made the test to changue the order of the arrays ejem:

sensorTemp[5]
sensorTemp[4]
sensorTemp[3]
sensorTemp[2]
sensorTemp[1]
sensorTemp[0]

And the result is the same; the array dont print the first 3 numbers, but the last 3 its ok.

if( saveToSD == true) 
  { 
    Serial.println(fechaStr ); 
    Serial.print("1.-"); Serial.println(sensorTemp[0] ); 
    Serial.print("2.-"); Serial.println(sensorTemp[1] ); 
    Serial.print("3.-"); Serial.println(sensorTemp[2] ); 
    Serial.print("4.-"); Serial.println(sensorTemp[3] ); 
    Serial.print("5.-"); Serial.println(sensorTemp[4] ); 
    Serial.print("6.-"); Serial.println(sensorTemp[5] ); 

      if(system_type == single_compressor || double_compressor)
      {
        dtostrf(sensorTemp[0], 5, 1,  CT1 );
        dtostrf(sensorTemp[1], 5, 1,  CT2 );
        dtostrf(sensorTemp[2], 5, 1,  CT3 );
        dtostrf(sensorTemp[3], 5, 1,  CT4 );
        dtostrf(sensorTemp[4], 5, 1,  CT5 );
        dtostrf(sensorTemp[5], 5, 1,  CT6 );

if(numSen == 6)
        {
          sprintf(line1_data, "   F%s  H%s  1%s  2%s  3%s  4%s  5%s  6%s", fechaStr, horaStr, CT1, CT2, CT3, CT4, CT5, CT6);      
          Serial.println(line1_data);
        }
      }

  File dataFile = SD.open("datalog.txt", FILE_WRITE);   //abrimos el archivo en la SD 

  if (dataFile)                                         // if the file is available, write to it:
  {  
    dataFile.println(line1_data);                    //save in the file the header 
    dataFile.close();

    lcd1.setCursor(0,1); lcd1.println("  SE HAN GUARDADO  ");
    lcd1.setCursor(0,2); lcd1.println("      LOS DATOS    ");
    lcd1.setCursor(0,3); lcd1.println("                   "); 
  }
  

  saveToSD = false;       //down flag 
  }
}//end save to sd dataline

I know they are snippets, the file it is in attachments;
Things I check before;

The sufficient length of the char [90] caracters is more than double.
The floats are in there, and are right
The sprintf function works in all the other uses, and works properly, but not in this case.

Any Idea of what might be ???

Thanks for the Insigths!

-Alex.

prog_prueba_promedio_sensores_CT.ino (28.8 KB)

the CTn strings are char[6] ?

  char CT1[5] = "";              //PARA CONVERTIR LA TEMPERATURAS A CHAR 
  char CT2[5] = "";
  char CT3[5] = "";
  char CT4[5] = "";
  char CT5[5] = "";
  char CT6[5] = "";
  char CT1[5] = "";              //PARA CONVERTIR LA TEMPERATURAS A CHAR 
  char CT2[5] = "";
  char CT3[5] = "";
  char CT4[5] = "";
  char CT5[5] = "";
  char CT6[5] = "";

Yes they are char, Im using them to convert the float decimal point:

A resumed code will be something like this:

//globals 
float  sensorTemp[0] = 20.4;              //this is the C temperature 
char CT1[5] = "";                              // this is char for convert the temperature 
char line1_data[95] = "";                   //a single line to merge temperature, date, time, and another data 

//in subfuction 
dtostrf(sensorTemp[0], 5, 1,  CT1 );   // in here we translate the float to char array 

and for that moment in foward, the only thing to do is to merge all data in a single line; 

sprintf(line1_data, "   F%s  H%s  1%s  2%s  3%s  4%s  5%s  6%s", fechaStr, horaStr, CT1, CT2, CT3, CT4, CT5, CT6);

as you mention each char CT1[5] = "";, char CT2[5] = "";, etc are small buffers to convert from float to a char

Once the float as transfered to a char, it is merged in one single row, using a sprintf...

The float data is present and correct, but as soon enters the last sprinf (to merge one in one row) The first 3 items are gone. Even more, for some reason has a jump on the print of the line.

I think I pass something over, but I can see a thing

-Alex.

I asked "the CTn strings are char[6] ?"
5 visible characters plus a terminating zero are 6 bytes

Juraj:
I asked "the CTn strings are char[6] ?"
5 visible characters plus a terminating zero are 6 bytes

You got abosolutley rigth!!!!

I knew was something so easy... thanks a million!!!

-Alex.