2

I have this code:

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


#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

TinyGPSPlus gps;
SoftwareSerial ss(GPS_RX_PIN, GPS_TX_PIN);

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

char clat[11];
char clng[11];

void loop()
{

        bool isGpsLocationValid = false;
        do
        {  
            while (ss.available()>0)
            {
                char c = byte(ss.read());
                if (gps.encode(c)) 
                {    
                    if (gps.location.isValid())
                    {
                        dtostrf(gps.location.lat(), 11, 6, clat);
                        dtostrf(gps.location.lng(), 11, 6, clng);
                        isGpsLocationValid = true;
                    }
                }   
             }
        } while (isGpsLocationValid == false);
        Serial.write(clat);
        Serial.println();
        Serial.write(clng);
}

Now when all is done, the clng value is printed twice in the serial monitor. The value i get is in this format:

27.275869  15.151013
  15.151013

As you can see, clng is printed twice. Any ideas why is that?

4

The width parameter to dtostrf() is the width of the resulting string NOT including the null terminator. So you are overflowing the array.

Change:

char clat[11];
char clng[11];

to

char clat[12];
char clng[12];

and it should work.

  • bear in mind, my number is this : 27.275869 . and i have declared 11 digits. i think they are more than enough. i will provide more feedback on this tomorrow – user1584421 Jun 20 '14 at 17:21
  • 2
    The width parameter is the minimum width not the maximum width. See the docs. If you have signed lat/long values you will need 11 characters plus the null terminator. Unsigned will only need 10 plus the null. – Craig Jun 20 '14 at 17:28
  • 2 digits + 1 sign + 1 dot + 6 decimal digits + 1 NULL = 11. Why do i need to declare more? – user1584421 Jun 21 '14 at 1:40
  • Because dtostrf will pad the result string with spaces so that it is at least 11 characters long, so you need 12 chars in your variable. Instead of arguing, you should just do it and see that it works with 12. – jfpoilpret Jun 21 '14 at 6:46
  • I did with up to 14 digits and the results remain the same – user1584421 Jun 21 '14 at 17:56
-2

OK, i found the problem. I guess this is an issue with the TinyGPS++ library. When using gps.location.lat(), the output is 27.275869 15.151013, instead of just 27.275869.

So when also using gps.location.lng(), i get the double longitute....

  • 3
    That shouldn't be possible. lat() and lng() each return a floating point number (hence why you're using dtostrf() to convert them to strings). It can't return two separate numbers in a single call. I think what's happening is that your char arrays aren't big enough, so one string is effectively bleeding into the next. – Peter Bloomfield Jun 20 '14 at 12:22
  • I will provide more feedback on this tomorrow, after i run some tests – user1584421 Jun 20 '14 at 17:20
  • Guys i stand my ground. Did tests today and no matter the size of dtostrf (from 11 i went up to 14), the results are the same, unless i only use Serial.write(clat); Its gotta be a library thing – user1584421 Jun 21 '14 at 13:02
  • 2
    What did you increase to 14? The char array, or the width parameter for dtostrf()? – Peter Bloomfield Jun 21 '14 at 18:14
  • The width parameter. Oh, i will also try to increae the char array too. – user1584421 Jun 21 '14 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.