 # Distance between two coordinates

Hey here, I want to know how to get a distance or time between two coordinates points (latitude and longitude) in arduino ! I have found a javascript code and further translated it in arduino, but it does not work ! I f someone can help me i will be thankful . Thanks

Your code seems to be missing...

http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe

Take your pick of languages, including C++

a javascript code and further translated it in arduino, but it does not work!

It would make sense to post the code, using code tags, and define "does not work", don't you think?

This is the code I have translated! the distance that is shown differs: d = 15.96 d = 7.54 I got those distances with the same geographic coordinates

// Finding the distance between two coordinates float lat2 = 31.318532, lon2= 75.630783; double r = 6378100;

float dLat = (lat2-flat) * 3.14 / 180; //degree to radian conversion float dLon = (lon2-flon) * 3.14 / 180;

float lat1 = (flat) * 3.14 / 180; float lat3 = (lat2) * 3.14 / 180;

float a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat3); float c = 2 * atan2(sqrt(a), sqrt(1-a)); float d = r * c; Serial.print(" a: "); Serial.print(a); Serial.print(" Distance: "); Serial.print(d);

So, you put in latitude and longitude with six decimal places of precision, but PI to only two?

One degree of latitude = roughly 110 Km

Note: "float" variables are accurate to 6-7 decimal places, and you need at least 9 decimal places of precision to do accurate distance calculations on the Earth's surface.

Or can I just copy The C or C++ code , then include it to a arduino? If yes, how could I do that?

Thanks.

Pitou: Or can I just copy The C or C++ code , then include it to a arduino? If yes, how could I do that?

Thanks.

No, you can't just copy it to the Arduino. You need to look at the code and adapt it.

For instance

``````static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
``````

Looks a little beyond what the average Arduino can cope with, but round it to less decimal places and you might have a chance.

Clueless programmers tend to do this sort of thing, because all those useless extra digits certainly look good, and lend false confidence in the final product!

``````static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
``````