unstable math results

Hello there,

I designing a gps tracker, that basically reads the distance and add this numbers in a float variable.

everything works fine until the variable result reaches "1000,00 meters". Sudendly the variable result returns to a value near "500,00 meters".

If you wanna take a look, the sketch is attached.

The variable that holds this value is the 'float Dis'.

I'm a medium coder and I've tried something to work it out.

Changed it to all other variable types, long, short, double. Nothing.

Changed the delay.

Added a decimal number to all the components of the math operation.

And at last, changed from UNO to MEGA 2560, wishing that it was a memory issue.

I'm sorry about my writing, not fluent.

Thank you in advance.

GPS_CALC_MEGA.ino (8.4 KB)

This declaration is a problem:

char serverString[5];

because when you store a floating point number in it:

        Data_tour = dtostrf(Dis,6,2,serverString);

You specify a field width of 6 characters but you've only allocated 5.
Also, the largest positive number you can print with a specification of 6,2 is only 999.99
If you want the number to be greater than that you have to increase the field width and the size of serverString.

Pete
P.S. There's a Portuguese language forum here.

This code is a mess, as you are mixing Strings (which we recommend to NOT use, because they cause memory problems and program crashes on Arduinos) and C-strings (character arrays).

I don't see how the following code can work at all, in part because you reuse the same character array for two different formatted results:

     SD_flat = dtostrf(flat,10,6,dataString);
      SD_flon = dtostrf(flon,10,6,dataString); //overwrites the SD_flat result
      SD_record = SD_flat + "," + SD_flon;

Same problem here:

      Data_tour = dtostrf(Dis,6,2,serverString);
      Data_speed = dtostrf(avar_speed,6,2,serverString);  //overwrites the Data_tour result
      Data_server = Data_tour + "m , " + Data_speed + "Km/h";

And this:

char serverString[5];

serverString can hold only 4 characters plus the zero terminator byte.

Thank you guys,

I'll make this modifications and see if it works.

SD_flat = dtostrf(flat,10,6,dataString);
SD_flon = dtostrf(flon,10,6,dataString);
SD_record = SD_flat + "," + SD_flon;

This code here, actually works fine. The same goes to this part here.

Data_tour = dtostrf(Dis,6,2,serverString);
Data_speed = dtostrf(avar_speed,6,2,serverString);
Data_server = Data_tour + "m , " + Data_speed + "Km/h";

I didn't come up with that. Used an example on how to convert float in to string.

But than you both told me about the

char serverString[5];

I undestood how it is a problem. But, the variable that's giving me the wrong results is a flot 'Dis'.
and I can see the math problem when I print it when it is still a float variable.

This conversion comes after, just to record it on SD card.

That said, even the char width [5] and string conversion in 6,2 going after the float print, it interfeers on it?

And I'm posting here, cause i feel there are more qualified people on here.

Thank you again.

You can lead a duck to water, but you can’t force it to understand what happens when a variable goes out of bounds...

I thought that was a horse. Ducks are smarter.