This was a personal project in which I created a physical computing prototype using the Arduino to help me play better golf! The basic idea is to measure the distance in yards between wherever I am and the current hole. The skills involved spanned a whole spectrum from electrical engineering, to coding, to mathematical formula creation. It was definitely the most difficult project I’ve worked on, but easily my most rewarding.
Creating the Prototype
This prototype reads in the coordinates from the current GPS fix and calculates the distance in yards from the GPS fix to a target location using a modified version of the Haversine formula.
- Arduino UNO
- Adafruit ultimate GPS
- Liquid-crystal LCD screen
- 9v Battery Adapter
This image was made with Fritzing:
Writing the code
Here is a link to the full code with detailed comments. Below are some of the harder concepts I had to work through in order to get the code working.
Finding the correct GPS data format
A GPS receives NMEA (National Marine Electronics Association) data of GPGGA and GPRMC. Without going into too much detail, GPRMC is the data you want to calculate distance because it parses in the signed degrees format. This format is very important for calculating short distances because obtaining latitude and longitude from a point on Google maps is the most accurate in the signed decimal format.
Small distance calculation with the Arduino
Logically I thought using the well documented Haversine formula would be the easiest approach to calculating distance. Oddly enough, I kept getting distance calculations that were consistently 500 – 700 yards off using the formula.
After a little research, I found that in order to calculate small distances accurately you need to have signed degrees that are accurate to about the 9th place after the decimal. Okay, why the problem?!
The Arduino only holds floating point numbers to the 7th place after the decimal.
After a little more web searching, I found a great post by JeonLab which describes a solution to this exact problem. Here is his modified formula which I used and tested to be an accurate calculation:
The mean circumference of the earth is 2 x 6,371,000m x π = 40,030,170m
Δd (lat) = 40,030,170 x ΔΘ (lat) / 360 (assuming ΔΘ is small)
Δd(long) = 40,030,170 x ΔΘ(long) x cosΘm / 360 (Θm: mean latitude between two positions)
Now, the distance is √[Δd (lat)^2 Δd (long)^2]