# GPS correct delay implementation

**URL:** <https://forum.arduino.cc/t/gps-correct-delay-implementation/925272>\
**Category:** Programming\
**Created:** [November 15, 2021, 1:53pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272 "2021-11-15T13:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubanl](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@hubanl](https://forum.arduino.cc/u/hubanl)\
**Post date:** [November 15, 2021, 1:53pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272/1 "2021-11-15T13:53:56Z")

</div>

Hey,

I connected the NEO-6M GPS module to an Arduino MKR NB 1500. When using the code below without a delay the altitude and number of satellites are printed correctly. However, when i insert a 2s delay the altitude and number of sattelites prints out 0 (see picture below). Can anyone tell me what i am doing wrong? Lat and Long values are blacked out for private reasons.

```cpp
#include "TinyGPS++.h"
TinyGPSPlus gps;
#define gpsSerial Serial1

void setup() {
  // initialize serial communications
  Serial.begin(9600);
  gpsSerial.begin( 9600 );

}

void loop() {

  while ( gpsSerial.available() > 0)
  {
    gps.encode(gpsSerial.read());
    }

  if (gps.location.isValid())
  {
    Serial.print("LAT="); Serial.println(gps.location.lat(), 6);
    Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
    Serial.print("ALT="); Serial.println(gps.altitude.meters());
    Serial.print("# of satallites="); Serial.println(gps.satellites.value());
    delay(2000);
    }
}

```

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/4/5/1/451ffea542494360eb1ffa36728f23e6f87daddb.png)

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 15, 2021, 1:59pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272/2 "2021-11-15T13:59:25Z")

</div>

when you add a delay, you stop feeding the beast

```cpp
gps.encode(gpsSerial.read());

```

the Serial buffer is small and at 9600 bauds, you get 960 characters per second... so you have now missed 1920 characters from the GPS and only have the last 64 in the buffer...

use millis() to drive your display only from time to time.

```cpp
#include "TinyGPS++.h"
TinyGPSPlus gps;
#define gpsSerial Serial1
unsigned long lastDisplayTime = 0;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);
}

void loop() {
  while ( gpsSerial.available() > 0)gps.encode(gpsSerial.read());

  if (gps.location.isValid() && (millis() - lastDisplayTime >= 2000)) {
    Serial.print("LAT="); Serial.println(gps.location.lat(), 6);
    Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
    Serial.print("ALT="); Serial.println(gps.altitude.meters());
    Serial.print("# of satallites="); Serial.println(gps.satellites.value());
    lastDisplayTime = millis();
  }
}

```

---

<div class="post-metadata">

**Author:** ![hubanl](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@hubanl](https://forum.arduino.cc/u/hubanl)\
**Post date:** [November 15, 2021, 2:22pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272/3 "2021-11-15T14:22:31Z")

</div>

Thank you so much Jackson for a clear explanation and answer to my question. U forgot the brackets in the while loop, but the code is working perfectly now!

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 15, 2021, 2:33pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272/4 "2021-11-15T14:33:34Z")

</div>

I did not forget anything as my while loop had only 1 statement, so I did not need the brackets to make a compound statement... 😉

but it's fine to add them and write

```cpp
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
  }

```

it does not change anything... (it's a good practice for some to add the brackets always)

PS: it's important to understand what are the different [statement is in C++](https://en.cppreference.com/w/cpp/language/statements), will make your life much easier

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 14, 2022, 2:34pm UTC](https://forum.arduino.cc/t/gps-correct-delay-implementation/925272/5 "2022-05-14T14:34:18Z")

</div>

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