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());
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.
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.
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.
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...
}
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.
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!
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...