calculation with ints to get Gps coordinates

Hello I'm trying to convert some gps data from DMS (degrees, minutes, secondes) >> to >> DD (decimal degrees)

Now I get the wrong value out. I think the problem is of wrong usage of the vars type, int and float.

part of my code is:

void saveGps(String coord) {
Serial.println("dagen: " + coord.substring(0));
Serial.println("minuten: " + coord.substring(2, 4));
Serial.println("seconden: " + coord.substring(5));

int day = coord.substring(0, 2 + n).toInt();
int min = coord.substring(2 + n, 4 + n).toInt();
int sec = coord.substring(5 + n).toInt();

float lat_out = (dagen + (minuten/60) + seconden) / (60*60);
Serial.println("Lat uit:");
Serial.println(lat_out,6);

Hello I'm trying to convert some gps data from DMS (degrees, minutes, secondes) >> to >> DD (decimal degrees)

Now I get the wrong value out. I think the problem is of wrong usage of the vars type: int and float.
The code compiles but it returns the wrong value..

part of my code is:

// DMS string in:
saveGps("5205.6295");

void saveGps(String coord) {    
    int deg = coord.substring(0, 2).toInt();
    int min = coord.substring(2 , 4).toInt();
    int sec = coord.substring(5).toInt();
   
    // convert to DD 
    float lat_dd = deg+ min / 60  + sec  /  (60*60);     /// !!!!! 
    Serial.println("Lat out:");
    Serial.println(lat_dd,6);   // give WRONG output: 1.763056 instead of 
}

Edit:

  • name var has to be deg instead of day..
  • n was for the long (has one more digit..).

I tested the values with:
Serial.println("deg: " + deg);

Have you verified that you are extracting the right data from the String? Why are degrees stored in a variable called day? Where is n defined?

You define "day" but then refer to it as "dagen" and the same problem for "min" and "sec".

The other problem is that you are doing the calculation with integers. For integer arithmetic in C/C++ there is no remainder, so 10/20 is zero - not 0.5.
You can either declare day,min, sec to be float, or force the calculation to be done with floating point:

float lat_out = ((float)dagen + (minuten/60.) + seconden) / (60*60);

Pete

You are dividing ints by ints. You do know that 59/60 == 0 ??

This is a duplicate post - please don't cross post like this is wastes everyone's time.

Thank you. I it has to do something with that ints..

So do i have to cast all parts a float? I will try.

I tried it without the seconds and it works. Thanks
float lat_out = ((float)deg) + (float)(min / 60.0);

Note that the float and double (which is the same) in Arduino generally has too little precision for GPS calculations.