I'm playing around with the nifty TinyGPS library from Arduinina, but quickly found myself trying to learn how to slightly modify the output of the GPS data I am getting. What I would like to do is save the formatted values into a buffer. Using the code included below, I'm getting the following results:
However, I'm trying to obtain the following formatted output:
Orig. HDOP: 340 Decimal HDOP: 3.4 HDOP_formatted: 03.40 Lat: + 24.304641 New Northern Lat value: +24.304641 Speed: 0.28 Formatted Speed: 00.28
I have a feeling that I'm on the write direction, but I'm very confused as I try to format the output....
Any ideas on what I could change to get the format I'm looking for?
////////////////////////////////////////////////////////////////////
//Print HDOP value (00.00) to a buffer
Serial.print("Orig. HDOP: ");
printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
Serial.print(" Decimal HDOP: ");
Serial.print(hdop2.value());
char hdop_string[5]; // this is to reformat the HDOP
snprintf(hdop_string, sizeof(hdop_string), "%2.2f", hdop2.value()); //use 2.2f to have a format of XX.XX
Serial.print(" HDOP_formatted: ");
Serial.print(hdop_string);
Serial.print(" ");
Your problem is that the Arduino IDE does not support floating point output when using snprintf() because support for it has been removed to save space, hence the ? in your output.
There are tricks you can play such as splitting your floating point number into 3 parts, integer part, decimal point and decimal part, turning the latter into an integer and then outputting the 3 parts as you have done with the speed.
It is also possible to reinstate support for floating point output but that will have implications on program size.
Thanks for the feedback. This raises one question, though: why am I seeing my speed values change when I do the formatting? And most importantly, how can I avoid this?
What sort of change do you see ? You could, of course, simply check the value of speed and print a zero if it is below 10.
Another question. As you are simply printing the values rather than putting them into one big char array why don't you just do this ? Serial.print(hdop2.value(), 2);
I've been experimenting with the approach that you suggested of breaking things up into "pieces" and that seems to be doing the trick so far... What you mentioned totally makes sense... I wish the Arduino could handle the floating values a bit easier. Oh well The pleasures of C/C++ programming, right?
As for your last suggestion, I need to save the formatted values in a buffer so that I can use them later. Thanks so much again... your description & suggestions really helped me get a much clearer view on how to tackle this!