Panel Cookies
GPS distance meter CODE
Help me by sharing this post

This is the code for the Arduino based GPS distance meter. Make sure you also install the needed library for the GPS module. Download that from below. Install the library, compile and uplaod the code to the Arduino. Downlaod the zip file or copy+paste from below. Read all comments in the code!





schematic arduino GPS 6M



#include <SoftwareSerial.h>
#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
   Code: http://www.electronoobs.com/eng_arduino_tut87_code1.php
   Schematic: http://www.electronoobs.com/eng_arduino_tut87_sch1.php
   SUBSCRIBE: http://youtube.com/c/ELECTRONOOBS
*/
TinyGPS gps;
SoftwareSerial ss(4, 3);
int battery = A0;

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

float start_lat, start_lon, flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;

int Altitude_S = 0;
int Altitude_F = 0;
int Altitude = 0;

void setup()
{
  analogReference(INTERNAL);      //Internal 1.1V reference
  pinMode(battery, INPUT);
  Serial.begin(115200);//or 9600 
  ss.begin(9600);
  gps.f_get_position(&start_lat, &start_lon, &age);  
}

void loop()
{
  
  
  if(Serial.available()>0)
  {
    String Received = Serial.readString();                //Save the received String in the Received variable
   
    if(int(Received[0]) == 0)                             //If the first character of "Received" is "0" //Distance start mode
    {
      gps.f_get_position(&start_lat, &start_lon, &age);      
      int latitude = start_lat;
      int longitude = start_lon;

      float lat_dec = (start_lat - latitude);
      lat_dec = lat_dec * 1000000;
      long lat_dec_int = long(lat_dec);
      //lat_dec_int = abs(lat_dec_int);
      
      float long_dec = start_lon - longitude;
      long_dec = long_dec*1000000;
      long long_dec_int = long(long_dec);
      //long_dec_int = abs(long_dec_int);

      
      
      
      Serial.print("log.txt=");                             //We print the "variable" we want to change on the screen   
      Serial.write(0x22);
      Serial.print("START point coordinates are stored");   //Print the "value" we want to be displayed
      Serial.write(0x22);
      Serial.write(0xff);                                   //Always add 3 full bytes after...       
      Serial.write(0xff);
      Serial.write(0xff);
      
      Serial.print("lat.val=");                             //Print the "variable" we want to change
      Serial.print(latitude);                               //Print the "value" we want to be displayed
      Serial.write(0xff);                                   //Always add 3 full bytes after...       
      Serial.write(0xff);
      Serial.write(0xff);
      
      Serial.print("lat_dec.val=");                         //Print the "variable" we want to change
      Serial.print(lat_dec_int);                            //Print the "value" we want to be displayed
      Serial.write(0xff);                                   //Always add 3 full bytes after...       
      Serial.write(0xff);
      Serial.write(0xff);

      Serial.print("long.val=");                 
      Serial.print(longitude);                 
      Serial.write(0xff);                                       
      Serial.write(0xff);
      Serial.write(0xff);

      Serial.print("long_dec.val=");                 
      Serial.print(long_dec_int);                
      Serial.write(0xff);                                       
      Serial.write(0xff);
      Serial.write(0xff);      
      smartdelay(1000);
    }
    

    if(int(Received[0]) == 1)                           //If the first character of "Received" is "1"   //distance finish mode
    {
      gps.f_get_position(&flat, &flon, &age);
      smartdelay(100);
      int distance = TinyGPS::distance_between(flat, flon, start_lat, start_lon);
      gps.f_get_position(&start_lat, &start_lon, &age);      
      Serial.print("distance.val=");                                                  //We print the "variable" we want to change on the screen
      Serial.print(distance);                                                         //Print the "value" we want to be displayed
      Serial.write(0xff);                                                             //Always add 3 full bytes after...       
      Serial.write(0xff);
      Serial.write(0xff);

      Serial.print("log.txt=");                          
      Serial.write(0x22);
      Serial.print("Distance between START and FINSIH was calculated");                 
      Serial.write(0x22);
      Serial.write(0xff);                                 
      Serial.write(0xff);
      Serial.write(0xff);
      smartdelay(1000);
      gps.stats(&chars, &sentences, &failed);
    }



    if(int(Received[0]) == 2)                             //If the first character of "Received" is "2"   //Altitude start mode
    {
      Altitude_S = gps.f_altitude();        
      Serial.print("log.txt=");                        
      Serial.write(0x22);
      Serial.print("Altitude START point was stored");                 
      Serial.write(0x22);
      Serial.write(0xff);                                      
      Serial.write(0xff);
      Serial.write(0xff);
      
      Serial.print("alt_s.val=");                
      Serial.print(Altitude_S);                
      Serial.write(0xff);                               
      Serial.write(0xff);
      Serial.write(0xff);
     
      smartdelay(1000);
    }


    if(int(Received[0]) == 3)                             //If the first character of "Received" is "3" Altitude finish mode
    {
      Altitude_F = gps.f_altitude();    
      Altitude = Altitude_F - Altitude_S;    
      Serial.print("log.txt=");                           
      Serial.write(0x22);
      Serial.print("Altitude FINISH point was stored and altitude difference calculated");                
      Serial.write(0x22);
      Serial.write(0xff);                                 
      Serial.write(0xff);
      Serial.write(0xff);
      
      Serial.print("alt_f.val=");                 
      Serial.print(Altitude_F);               
      Serial.write(0xff);                                   
      Serial.write(0xff);
      Serial.write(0xff);

      Serial.print("altitude.val=");                 
      Serial.print(Altitude);                 
      Serial.write(0xff);                                
      Serial.write(0xff);
      Serial.write(0xff);     
      smartdelay(1000);
    }








    
  }//end if serial available



  int battery_level = map(analogRead(battery),814,977,10,100);     
  battery_level = constrain(battery_level,10,100);   
  //977 analog read = 4.2V using a divider of R2=10K and R1=30K; 812 = 3.5V (low battery)
  Serial.print("battery.val=");               //We print the variable we want to cahnge on the screen
  Serial.print(battery_level);                //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
  

  int satelites = gps.satellites();
  if(satelites < 50)
  {
    Serial.print("satelites.val=");               //We print the variable we want to cahnge on the screen
    Serial.print(satelites);                      //Print the value we want to be displayed
    Serial.write(0xff);                           //Always add 3 full bytes after...       
    Serial.write(0xff);
    Serial.write(0xff);
  }

  
  smartdelay(1000);


  

}




//GPS functions. See TtinyGPS library example for more

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartdelay(0);
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartdelay(0);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);
}













yt_link
insta_link
fb_link
twitter_link

GPS code



Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo