GPS Sensor (with TinyGPS) Problem

Hello everyone,

I am new to the Arduino world (though not to Programming) and I'm really liking it, but lately I came across a really strange behavior of my Sketch:

I am using a Arduino UNO with a NEO M6 GPS-Chip.
I am reading the Data from the GPS-Module with the SoftwareSerial library then passing the characters to a TinyGPS-Object to parse the Data.

I want this to work as a function which only reads/prints Data when it is called.
My code looks like this (gpsSerial ist the SoftwareSerial object, gps ist the TinyGPS object):

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

void loop() {

    Serial.println(getSpeed());
    delay(2000);  

}

float getSpeed(){
  while (!gpsSerial.available()){
    //wait
  }
  int c = gpsSerial.read();
  while(!gps.encode(c)){
    c= gpsSerial.read();
  }
  return gps.f_speed_kmph();
  
}

Well this does not work. If I print the result of this function to the Serial Monitor nothing happens. But then I wanted to check if one of the loops were endless for any reason, so I added two Serial outputs:

float getSpeed(){
  while (!gpsSerial.available()){
    Serial.println("1");
    //wait
  }
  int c = gpsSerial.read();
  while(!gps.encode(c)){
    Serial.println("2");
    c= gpsSerial.read();
  }
  return gps.f_speed_kmph();
  
}

Now the Serial Monitor shows a number of twos and (and that's the strange thing) the desired value (in this case speed, but it also works like this for the position or anything else).

Has anyone an Idea what that could be and how to resolve it??

Thanks!

The GPS probably doesn't have a fix. For a test, just echo the characters from the GPS to the serial monitor.

Okay I did that. This was the result:

$GPGSA,{removed original Information}
$GPG3,{removed original Information},(Are these supposed to be there ->)ÿÿ,ÿ,{removed original Information}
$GPGSV,{removed original Information}
$GPGSV,{removed original Information}
$GPGSV,{removed original Information}
$GPGSV,{removed original Information}
$GPGLL,{removed original Information}
$GPZDA,{removed original Information}
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ$GPRMC,{removed original Information}

So there is data coming in from the Sensor...
P.S.: Does anyone know sth. about these ÿ's? They don't seem to be right...

You did not do yourself a favor by cutting out part of the output.

Did the "original information" consist of normal ASCII NMEA sentences? If so, did they contain valid data?

Post the code that produced the problematic output.

Well yes, the NMEA-Strings seemed to look valid and the Coordinates were correct (that's why I cutted them out of course...).

By now I found a quite handy workaround for this strange Problem which looks like this:

void getPos() {
    while (true) {
      while (nss.available())
      {
        int c = nss.read();
        if (gps.encode(c))
        {
          gps.f_get_position(&pos[0], &pos[1]);
          return;
        }
      }
    }
}

It works just finde, so what really caused this strange problem might always stay a mystery...

But thanks a lot anyway!!! :slight_smile: