GPS or Global Positioning System is a satellite-based radio navigation system which allows you to get your location and guide you through other locations through a well recognized and predefined map like Google maps, and in the world of Arduino, this is accomplished by the Arduino GPS Shield.

The GPS knows your location through the latitude and longitude values of your location which specifies where exactly are you from the world and we are going to use these two measurements to calculate the distance between your current location and the desired destination using the GPS shield on 1Sheeld in a quick and funny Arduino GPS Shield Tutorial.

Let’s talk about the idea behind this Arduino GPS Shield Tutorial ...



Idea:

In the Arduino GPS Shield Tutorial, we are going to use the GPS Shield from 1Sheeld via its companion Android/iOS App to get the current location.

We achieve this by telling the App (by using the voice recognition shield), both the latitude and longitude of the desired location we want to reach and the Arduino will calculate the direct distance between the 2 locations in km unit (by using the GPS shield) and tells you (by using the text-to-speech shield) what is the distance.


Getting started:

If this is your first time to deal with 1Sheeld or you want to learn more about it, I recommend checking this quick and easy getting started tutorial.

Now, after you've become a little bit familiar with 1Sheeld, let's start!


Step 1: Hardware components:

-       Arduino Uno.

-       1Sheeld+ board.

-       Arduino USB cable or 9-12v battery.

-       Android/iOS phone with 1Sheeld App installed on it.


Step 2: Software components:


Step 3: Connection and Schematic:

Firstly, you slide the switch towards the “SWITCH” notation which turns the 1Sheeld board into the Uploading mode to let you upload the Arduino code.

Secondly, after you finish uploading the code, slide the switch towards the “UART” notation (or “SERIAL” at 1Sheeld+ board) which turns the 1Sheeld board into the Operating mode to communicate with your smartphone 1Sheeld App.


Code:

I would recommend checking the Arduino GPS Shield documentation to know more about the Arduino GPS Shield functionalities and how to use them.

Now, switch the 1Sheeld board to the Uploading mode, upload this code:

/*
  Arduino GPS Shield Tutorial: Distance Calculator

  This project shows an application on 1Sheeld's GPS shield.

  By using this project, you can calculate the distance 
  between your current location and the desired destination 
  by using Accelerometer shield from 1Sheeld.
  
  OPTIONAL:
  To reduce the library compiled size and limit its memory usage, you
  can specify which shields you want to include in your sketch by
  defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define.
*/

/* Include required shields */
#define CUSTOM_SETTINGS
#define INCLUDE_VOICE_RECOGNIZER_SHIELD
#define INCLUDE_TEXT_TO_SPEECH_SHIELD
#define INCLUDE_GPS_SHIELD
#define INCLUDE_TERMINAL_SHIELD

/* Include 1Sheeld library. */
#include <OneSheeld.h>

/* Variable to save number of voice commands received */
int counter = 0;

/* Char array to hold the voice recognized latitude */
char *voiceinput_latitude_char;

/* Char array to hold the voice recognized longitude */
char *voiceinput_longitude_char;

/* Float variable to hold the voice recognized latitude */
float voiceinput_latitude;

/* Float variable to hold the voice recognized longitude */
float voiceinput_longitude;

/* Float variable to hold the calculated distance in km */
float distance;

/* Char array variable to hold the calculated distance in km */
char distance_char[10];

/* String variable to hold the calculated distance in km */
String distance_string;

void setup () 
{
  //Initialize the communication between 1Sheeld's Arduino Voice Recognition Shield and Arduino
  OneSheeld.begin();
}

void loop ()
{       
        /* check if 1Sheeld's Arduino Voice Recognition Shield received a new command,
            this will be alwayes true after the first command received */
        if(VoiceRecognition.isNewCommandReceived())
        {
          /* Enter this block of code first to get the latitude */
          if(counter == 0)
          {
            /* Print to the Terminal shield */
            Terminal.println("getting latitude ....");

            /* Get the latitude value as char array */
            voiceinput_latitude_char = VoiceRecognition.getLastCommand();

            /* Convert the latitude char array value into float */
            voiceinput_latitude = atof(voiceinput_latitude_char);

            /* Without the counter, the code will go inside the next getLastCommand function 
              which will return the same previous received command */
            /* So, we need to increment the counter to force the code to enter the isNewCommandReceived 
               which will remove the previous command saved by the getLastCommand function */
            counter++;

            /* Print to the Terminal shield */
            Terminal.println("latitude received");
            /* now we got the latitude data and counter is 1 */
          }

          /* Enter this block of code secondly to get the latitude */
          else if(counter == 1)
          {
            /* Print to the Terminal shield */
            Terminal.println("getting longitude ....");

            /* Get the longitude value as char array */
            voiceinput_longitude_char = VoiceRecognition.getLastCommand();

            /* Convert the longitude char array value into float */
            voiceinput_longitude = atof(voiceinput_longitude_char);
            counter++;

             /* Print to the Terminal shield */
            Terminal.println("longitude received");
            /* now we got the longitude data and counter is 2*/
          }
        }
        if(voiceinput_latitude != 0 && voiceinput_longitude != 0 && counter == 2)
        {
          /* Print the latitude on the Terminal shield */
          Terminal.print("latitude: ");
          Terminal.println(voiceinput_latitude);

          /* Print the longitude on the Terminal shield */
          Terminal.print("longitude: ");
          Terminal.println(voiceinput_longitude);

          /* Increment the counter to prevent the program from repeating */
          counter = 3;
          
          /* Calculate the distance between the current location of the phone 
              and the voice-entered location */
          distance = GPS.getDistance(voiceinput_latitude,voiceinput_longitude);

          /* Convert distance into km unit */
          distance =  distance / 1000;

          /* Print the distance on the Terminal shield */
          Terminal.print("distance: ");
          Terminal.print(distance);
          Terminal.println(" km");

          /* Convert distance from float to char array */
          dtostrf(distance, 6, 3, distance_char);

          /* Convert distance from char array to string */
          distance_string = String(distance_char);

          /* Build this string and start the text-to-speech on it */
          TextToSpeech.say("you are about " + distance_string + " kilo meter from the ramsis train station");

          /* Delay 5 seconds to allow the speech be completed */
          OneSheeld.delay(5000);
        }
}   

Then, Switch the 1Sheeld board to the Operating mode then open the 1Sheeld app and connect it to the 1Sheeld board via Bluetooth.


Step 5: Run it:

As you see in the Arduino GPS Shield Tutorial video, you should select the GPS, Terminal, Text-to-speech and Voice recognition shields.

Once you navigate to the voice recognizer shield and tell your phone the location you want in terms of latitude and longitude values, it will calculate the distance between the current location and the location related to the entered latitude & longitude and tells you loudly the distance and also written in the Terminal shield tab.

In this tutorial, I wanted to know the distance between my current location "Integreight company" and the Ramsis Train Station in Cairo’s downtown and it was 8.327 km and I calculated it from Google maps too where the error was so little (Google maps distance is: 8.22km according to the screenshots below).

That was it, guys. I hope you enjoyed this Arduino GPS Shield Tutorial and for any questions or even opinions about it please don’t hesitate to leave your comments down below. See you with a new short and simple Arduino tutorial ;)