## Posted By

jatkins on 10/02/09

# ARDUINO: Distance between two points

/ Published in: Processing

uses the haversine formula

`float london[2] = {51.507222,-0.1275};float nyc[2] = {40.757267,-73.985839}; float radius_of_earth = 6378.1; // km void setup() {  Serial.begin(9600);} double distance_in_km(float start_lat, float start_long, float end_lat, float end_long) {  // / 180 / PI converts degrees to radians  start_lat/= 180 / PI;   start_long/= 180 / PI;  end_lat/= 180 / PI;  end_long/= 180 / PI;  // haversine formula - based on implementation at http://www.movable-type.co.uk/scripts/latlong.html  float a = pow(sin((end_lat-start_lat)/2), 2) + cos(start_lat) * cos(end_lat) * pow(sin((end_long-start_long)/2), 2);  float answer = radius_of_earth * 2 * atan2(sqrt(a), sqrt(1-a));  return double(answer);} void loop() {  Serial.print("Distance between Times Square, NYC and London: ");  Serial.print(distance_in_km(london[0], london[1], nyc[0], nyc[1]));  Serial.println(" km");  delay(1000);}`