Best way to convert GPS coordinates to decimal in my project.

Status
Not open for further replies.

merdenoms

New member
Hello, this is my first post and first project with the Teensy 3.x
My programming is rusty since I haven't done anything in a year. I decided to work on a project using GPS.

I have a PMB-648 GPS module, small LCD, and a few buttons wired up to my Teensy 3.0.
Depending on the button pressed, latitude or longitude coordinates will be displayed.
I'm excited that it actually works! haha

I am wanting to add more features to it and the first thing I need to do is convert the coordinates that are being displayed to proper decimals numbers.
I have been searching Google all night with no luck.

For example, take the number 64424751.
If there were a proper gps coordinate, it should actually look like 64.424751

Is there an insert() function or something I can use in C to just insert a "." after the first two characters?

I can also get the correct number by dividing the non decimal number by 1000000
For example, 64424751 / 1000000 = 64.424751
But when I output this to my LCD, it will only display 4 characters like 64.42
Is there a way I can tell it to output all of the characters after the decimal?

Below is an example of my code and a picture of my project.

http://pastebin.com/r88gq8xM

gBM6pk6.jpg
 
Hi,

Code:
long lat,lon;
long is an integer type, it will truncate to the nearest lower integer when you do the division and should display no decimal point or decimal places after the point at all. You could copy those values into a float after each update from the gps and then use the floats to display.
 
Try something like:

Serial.printf("%2.8f\n", (double)(64424751 / 1000000.0L));

or

Serial.printf("%d.%d\n",64424751 / 1000000, 64424751 % 1000000);
 
Last edited:
Thanks guys!

It worked by changing it to the below code.
But now I have another problem... if a coordinate ends up being a negative number, it will place a negative sign after the decimal.
For example, if the coordinate is "-9516891", it will output "-95.-1689" onto the LCD screen.
Any ideas on what format specifier I could use to fix this?

// Display GPS position on button press
if (digitalRead(6) == LOW) {
lcd.begin(0, 2);
lcd.print("lat: ");lcd.printf("%d.%d",lat / 1000000, lat % 1000000);lcd.print(" "); // print latitude
}
 
The second number, the decimal part should always be printed with 6 digits and leading zeros if smaller than 100000.

lcd.printf("%d.%06d",lat / 1000000, abs(lat % 1000000));
 
Status
Not open for further replies.
Back
Top