Convert string of variable length to floats.

Hello!
At the moment my problem is fix the drawpixel function to prevent it from being drawn lines ranging from 0 hour/6 as shown in the image below:

I want the chart that look like this:

The error should be in this code snippet, but I do not know how to write it correctly.

 for (uint8_t j=0; j <=(hora/6) ;j++)
 {
 setFont(SMALL, 255, 0, 255, 0, 0, 0);
 myGLCD.drawPixel((40+j),grafico);
  }

Thank you.
Fernando Garcia

Hey PaulS, I count 7 characters here - "nn.nn"

Yeah, yeah. I don't know why I was having trouble counting.

But the array should definitely be 8 characters long so the OP can null terminate the string.

Agreed. And OP should read the correct number of characters AND null terminate the string.

At the moment my problem is fix the drawpixel function to prevent it from being drawn lines ranging from 0 hour/6 as shown in the image below:

You are building a house on a crooked foundation, and you can't the shingles on the roof to line up properly. I think it is obvious what you need to do. Fix the damned foundation first.

Come on back AFTER you have that done.

Hello!
Thank you for all replies.
Already managed to fix the problems.

  int x, y, z, grafico;
  int16_t n;
  char buf[7];
  float temperatura;
  int j=0;
  file.open(&root, "LOGTDIA.TXT", O_READ);
  while ((n = file.read(buf, sizeof(buf))) > 0)
 {
if (strlen(buf)==9)
{
 j++;
}
 temperatura = atof(buf);

 if ((temperatura) < 22.5)
 {
 grafico = 190;
 }
 else if ((temperatura) >27.5)
 {
 grafico =30;
 }
 else
 {
 grafico = (190-((temperatura-22.5)*30));
 } 
 setFont(SMALL, 255, 0, 255, 0, 0, 0);
 myGLCD.drawPixel((39+j),grafico);
}
 file.close();
}

Hug.
Fernando Garcia

  char buf[7];
  while ((n = file.read(buf, sizeof(buf))) > 0)
if (strlen(buf)==9)

strlen() is a string function. It, therefore, expects a NULL terminated array of chars. Unpredictable results can be expected if you pass it something other than a string.

Leaving aside the fact that you refuse to properly NULL terminate the string, how do you expect to get strlen() to measure that there are indeed 9 characters in the 7 element array?

 temperatura = atof(buf);

atof() is also a string function. It, therefore, expects a NULL terminated array of chars. Unpredictable results can be expected if you pass it something other than a string.

PaulS:

  char buf[7];
  while ((n = file.read(buf, sizeof(buf))) > 0)
if (strlen(buf)==9)

strlen() is a string function. It, therefore, expects a NULL terminated array of chars. Unpredictable results can be expected if you pass it something other than a string.

Leaving aside the fact that you refuse to properly NULL terminate the string, how do you expect to get strlen() to measure that there are indeed 9 characters in the 7 element array?

 temperatura = atof(buf);

atof() is also a string function. It, therefore, expects a NULL terminated array of chars. Unpredictable results can be expected if you pass it something other than a string.

Hello!

I don't know to explain why the length of the string is equal to 9.
Did a Serial .print (strlen(buf)) and the program returned 9 for all strings.
As for the NULL terminator, the program recognizes the end of the string even without it.
If I have problems I change the program and acrescentor the terminator.

As for the NULL terminator, the program recognizes the end of the string even without it.

Bullshit! It is the lack of the NULL terminator that causes strlen() to return the wrong answer.