Storing GPS data in variables

So im using GY-NEO6MV2 GPS Module and an Arduino UNO for my project. Im using TinyGPS++ library v. 0.92 to get data from the module. I can print the location data to serial monitor without a problem with the example skecth(FullExample). But im having trouble storing data to variables. If i try to store them to a float variable like this

float lat = gps.location.lat();
float long = lat = gps.location.lat();

and print them to serial monitor i can’t get the full data. Instead of printing something like this 00.000000 it prints 00.00

So i tried using dtsurf function to store them in Strings. And i changed example sketch a little bit.

This is the code

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

static const int RXPin = 6, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);
}

void loop()
{
  Serial.println(printFloat(gps.location.lat(), gps.location.isValid(), 11, 6));
  Serial.println(printFloat(gps.location.lng(), gps.location.isValid(), 12, 6));


 
  smartDelay(1000);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
}

// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

String printFloat(float val, bool valid, int len, int prec)
{
  char out[15];
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    //Serial.print(val, prec);
    dtostrf(val, 8, 6, out);
    return out;
  }
  smartDelay(0);
}

when i try to print them like this. It prints characters randomly. After few seconds it freezes.
I want to do something like this.

Serial.println(lat);

And use that variable in other places.

Instead of printing something like this 00.000000 it prints 00.00

Serial.print() floats to 2 decimal places by default but you can do this

Serial.print(lat, 6);

Guess how many decimal places you get printed ?

See Serial print

I know that but i want to send that data with a gsm module. Thats why i need to store it. I already can print it like normal.

Are you saying that if you do this float lat = gps.location.lat();then lat has only 2 decimal places even if you use a length parameter when printing it ?

No. I dont need to print data to serial monitor i just want to store it and send it with gsm module.

sertis: I know that but i want to send that data with a gsm module. Thats why i need to store it.

You have stored it, its in the float variable lat.

So how do i send that. I can send only one with this

sim.println(lat,6);

But how to send lat and lng in one sms And i need to insert that data in a string. Like this

String message = "Lat: " + lat + " Lng: " + lng
sim.println(message);

Thats what i want to do

i need to insert that data in a string.

No, you don't. This works:

sim.print("Lat: ");
sim.print(lat,6);
sim.print(" Lng: ")
sim.println(lng,6);

Study some of the Arduino tutorials, in order to learn the programming language and special features of the Arduino. Save yourself endless frustration.

Why so you bother converting to a float in the first place?
They came off the GPS as strings - why not leave them as strings?

sertis,

I was having a similar problem.

I'm now using this method to send the data as text to Bluetooth and it produces what you are aiming for:

btSerial.print(String(gps.location.lat(),7) + delim); btSerial.print(String(gps.location.lng(),7) + delim);

delim is ","

However, others recommend against using the String type, so keep a close eye on it. At one point printing strings printed values stored as constants within the sketch instead of the values I wanted. The sketch was retrieving the wrong data.

John.