Multiple GPS Outputs for a 1 second update

I am able to read and parse my gps data. My issue is, I'm getting multiple outputs from a once-a-second read of the gps.
Any guidance would be appreciated.

I'm getting this (watch the seconds):

  • latitude: 39.63

  • longitude: -78.09

  • altitude: 793.96

  • speed: 0.00 km/h

  • GPS date&time: 2024-3-8 16:54:22.00

  • latitude: 39.63

  • longitude: -78.09

  • altitude: 793.96

  • speed: 0.00 km/h

  • GPS date&time: 2024-3-8 16:54:22.00

  • latitude: 39.63

  • longitude: -78.09

  • altitude: 793.96

  • speed: 0.00 km/h

  • GPS date&time: 2024-3-8 16:54:22.00

  • latitude: 39.63

  • longitude: -78.09

  • altitude: 793.96

  • speed: 0.00 km/h

  • GPS date&time: 2024-3-8 16:54:22.00

This is my loop:

void loop() {
if (gpsSerial.available() > 0) {
if (gps.encode(gpsSerial.read())) {
if (gps.location.isValid()) {
Serial.print(F("- latitude: "));
Serial.println(gps.location.lat());

    Serial.print(F("- longitude: "));
    Serial.println(gps.location.lng());

    Serial.print(F("- altitude: "));
    if (gps.altitude.isValid())
      Serial.println(gps.altitude.feet());
    else
      Serial.println(F("INVALID"));
  } else {
    Serial.println(F("- location: INVALID"));
  }

  Serial.print(F("- speed: "));
  if (gps.speed.isValid()) {
    Serial.print(gps.speed.mph());
    Serial.println(F(" km/h"));
  } else {
    Serial.println(F("INVALID"));
  }

  Serial.print(F("- GPS date&time: "));
  if (gps.date.isValid() && gps.time.isValid()) {
    Serial.print(gps.date.year());
    Serial.print(F("-"));
    Serial.print(gps.date.month());
    Serial.print(F("-"));
    Serial.print(gps.date.day());
    Serial.print(F(" "));
    Serial.print(gps.time.hour()-5);
    Serial.print(F(":"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
if (gps.time.centisecond() < 10)
    Serial.print(F("0"));
Serial.println(gps.time.centisecond());

  } else {
    Serial.println(F("INVALID"));
  }

  Serial.println();
}

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your whole sketch

Welcome to the forum!

I am not seeing anywhere in this code where timing is implemented to read the GPS output once per second? The only delay would be the time it takes to Serial.print the output.

You might want to look at the millis() command to time your read interval.

Showing us your Serial Monitor output, with timestamps enabled, would also be informative.

Yea...I thought about that. When the loop completes, it goes back to the beginning of the loop function where it says,

if (gpsSerial.available() > 0) {
if (gps.encode(gpsSerial.read())) {
if (gps.location.isValid()) {
Serial.print(F("- latitude: "));
Serial.println(gps.location.lat());

So, basically the loop is getting called like 4 times a second.

You are correct....there is no timing implemented.

The loop runs indefinitely with the timing dependent on how long it takes to execute the statements within it, which could range from a few microseconds to 100s of milliseconds or more. A simple and tempting option would be to put delay(1000) at the end of the loop which will give you a 1 second delay although plus the time it takes to execute the code. Delay also has the disadvantage that nothing else can happen during the delay. Using the millis() function would be more accurate and not block the execution of code while the timer is running.

This video explains how millis() can be used as a timer to fire a function at a given interval.

^https://www.youtube.com/watch?v=dCku8BabPx4

I found it helpful myself.

1 Like

Bit Seeker, Thank you very much ! I will go watch it.

I was thinking of using a goto statement AFTER the first run thru the code....I know its frowned upon...but using it once does not spaghetti make : )

So, what I'd do is make a holder (variable) for the "seconds" after the first run thru...
Then check to see if the seconds from "this next run" run equal the last run...if it does, then check_seconds_again();
{
do stuff...
}

poormans loop !

As previously requested, please post your full sketch, using code tags when you do. At the moment we don't even know which GPS library you are using

Helio...I'm good.

Thanks.

Assuming you are using TinyGPS++, spend some time with the documentation.

Not only do you need to check whether the location is valid, you also need to check the age of the location information. GPS signals can drop out for a significant period of time, and the library will still consider the data to be valid.

See the Validity, Update status, and Age section.

1 Like

Posting the full sketch is good advice from UkHeliBob. Always a good idea to do that because it takes much of the guesswork away for those trying to help. Its not unusual for the problem to be in the bit of code that has NOT been posted!

Remington....yea...I'm there...

I wrote some code in 2014 in C# for a Flight Management System, providing steering for manned aircraft and firing Mapping cameras on pre-determined Lat/long coordinates.
Every language is a little different.
....you might like...

Most GPSs put out around 8 NMEA sentences a second, but only two of them contain fix\location data.

But your code prints out the lat,lon,time for all of these 8 sentences, so for around 6 of the printouts the lat,lon,time will not have changed.

1 Like

Well, Srnet...you get the prize ! That's exactly what's going on.

I think what I'll do is just grab each string coming in, and check for the GPGGA string, and parse to get what I want.

Srnet...that was it. Now I just look for GPGGA string and ignore the rest.

String sGPRMC = gpsData.substring(0, 5);
if (sGPRMC == "GPGGA")
{

Thank you for your input !

No need for that, you can use TinyGPSplus library functions to work out if the GPGGA and GPRMC sentences have been recieved and encoded;

if (gps.speed.isUpdated() && gps.satellites.isUpdated()) //ensures that GGA and RMC sentences have been received and updated
    {

Speed is in GPRMC and satellites is in GPGGA

1 Like

Yea...I saw all that....
Today's library's really simplify getting things done.
I prefer writing it myself...for the most part.

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