I based my code using the JavaScript examples shown here:
http://www.movable-type.co.uk/scripts/latlong.html
You have to decide how often to aggregate the samples. I’m using 1/16th of a mile right now, but this might need tweaking as I get a better idea of the accuracy over time.
My code for the Arduino looks like:
#define MILES_PER_METER 0.00062137f
#define EARTH_RADIUS_METERS 6372795.0f
:
float distance = DistanceBetween2Points( latitude, longitude, f_lat, f_lon, MILES_PER_METER );
:
//************************
// Great Circle distance calculation
// Returns the distance between two lat/lon points on this great Earth
// (Note: Assumes sea level, does not take into account altitude. Close enough.)
//
float DistanceBetween2Points( float Lat1, float Lon1, float Lat2, float Lon2, float unit_conversion )
{
float dLat = radians( Lat2 - Lat1 );
float dLon = radians( Lon2 - Lon1 );
float a = sin( dLat / 2.0f ) * sin( dLat / 2.0f ) +
cos( radians( Lat1 ) ) * cos( radians( Lat2 ) ) *
sin( dLon / 2.0f ) * sin( dLon / 2.0f );
float d = 2.0f * atan2( sqrt( a ), sqrt( 1.0f - a ) );
return d * EARTH_RADIUS_METERS * unit_conversion;
}