Go Down

Topic: How to return output of Serial.print(floatNum,prec) in a function ? (Read 79 times) previous topic - next topic

unixbox911

Hi,

I am working on NEO6MV2 GPS module where I want to store coordinated in float variables. I am using TinyGPS library.

In order to do that, first I want to add extra decimal places in float value. This can be achieved by

Serial.print(12.224, 5) (https://www.arduino.cc/en/Serial/Print)

However as I said I want to store this result in variable, how can I do that ?

I cannot make

Code: [Select]
return Serial.print(12.224, 5);

This is the original function:

Code: [Select]

float print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartdelay(0);
}


Instead of directly printing the data on serial monitor, how can I store its output in a variable ?

Need help,

Thanks :-)

Delta_G

You don't get to define the number of decimal places of precision for a float variable. They always have all the decimal places no matter what you do. It is only for printing them out you get to decide how many to print, but they're always there.

For GPS coordinates, fixed point math with unsigned long is between 100 and 1000 times more precise. Why throw away all that precision on a float just to get slower more bulky code? 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

Delta_G

There's a difference between an actual number which always has an infinite number of digits behind the decimal and the written representation of a number which doesn't. What is stored in the computer is the actual number, with an infinite number of digits behind the decimal, although only a few of them may actually be accurate.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

unixbox911

I agree but in the library, here is the code for calling it:

Code: [Select]

float flat, flon;
gps.f_get_position(&flat, &flon, &age);
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);


When I write

Code: [Select]

Serial.print(flat)


before
Code: [Select]

print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);


it gives me float value like nn.dd. I want it as nn.dddddd

Thats my pain :-(

Delta_G

Code: [Select]
Serial.print(flat, 5);

Will print it to 5 places, but don't let that fool you to how many are in the number. You're just selecting how many to print.

I'd rewrite that library to use fixed point math if I were going to use GPS.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

unixbox911

I want to send data to HTTP server using SIM900, so I am concatenating string as>

Code: [Select]

String message;
message = "lat=";
message += flat;
Serial.println(message);


which is printing lat=nn.dd and not lat=nn.dddddd which I want

Delta_G

Oh you want it in a String.  Why didn't you just say that.

I don't know how to get precision for the String class. I try to avoid that when possible because it has a nasty habit of crashing Arduino code. To get it in a c style string use dtostrf
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

unixbox911

Hi,

Yes, all I needed was dtostrf(), problem solved.

Thanks  :)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy