Measuring distance using TinyGPS+

I am trying to create simple bike speedo with trip meter that uses GPS.
I have problem though with distance calculating

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

static const int RXPin = 8, TXPin = 7;
static const uint32_t GPSBaud = 9600;
double lastLat;
double lastLng;
double tripT = 000.0;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);
}

void loop()
{
  while (ss.available() > 0)
    if (gps.encode(ss.read())){
      storeCurrentLoc();
      delay(1000);
      displayTrip();
}


void displayTrip(){

  double tripA =
      TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      lastLat, 
      lastLng) / 1000;
      tripT = tripT + tripA;


  Serial.print("Trip:");
  Serial.print(" ");
  Serial.println(tripT);

  
}

void storeCurrentLoc(){
  lastLat = gps.location.lat();
  lastLng = gps.location.lng(); 
}

Unfortunately no matter how fast and how I go the trip shows always 0.
Speed, Time, long and lat etc is displayed correctly, speed rises and slows, lat and long changes with the location but Trip is always 0.

Is it because 1s is to small of a time for it to change?

Why not accumulate in native units, and do the division at the end?

You throw characters from serial at the tinyGPS object until you have a fix and store the lat long. Then you wait a second. Then you get the lat long from the object again and compare to what was stored.

While all that was going on, you didn't read from the GPS, so the stored and current pos will always be the same.

Not sure what you mean - I used example from the TinyGPS+ library here

@wildbill I did that so that for the first time when loop is run the long and lat would not be set to 0 - any sugesstions how to improve the code here ?

    if (gps.encode(ss.read())) {
      if (lastLat == 0.0 || lastLng == 0.0)
        storeCurrentLoc();
      displayTrip();
    }

and

  double tripA =
      TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      lastLat, 
      lastLng) / 1000;

      storeCurrentLoc();
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.