"Save" Longitude / Latitude as a variable

Hi folks,
I am trying to make my own Antenna Tracker. At the moment I struggle with saving the longitude and latitude as a variable.

#include <Adafruit_GPS.h>
//   TX - RX // RX - TX!!!
#include <TinyGPS++.h>
#define GPSSerial Serial1
#define XBeeSerial Serial2
Adafruit_GPS GPS1(&GPSSerial);
TinyGPSPlus GPSGround;
TinyGPSPlus GPSAir;

long LongitudeCopter = 0;
long LongitudeHome = 0;
long LatitudeCopter = 0;
long LatitudeHome = 0;
long AltitudeCopter = 0;
long AltitudeHome = 0;
long DeltaAltitude = 0;
float flatHome,flongHome,flat,flong;
float fDistanceBetween = 0;
float g_fCourseTo = 0;
long dAngleAlpha = 0;


void setup()  
{

  Serial.begin(115200);
  GPS1.begin(9600);
  GPSSerial.begin(9600);
  XBeeSerial.begin(19200);
  GPS1.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS1.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
  GPS1.sendCommand(PGCMD_ANTENNA);
  delay(1000);
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (GPSSerial.available() > 0)
    if (GPSGround.encode(GPSSerial.read()));
  while (XBeeSerial.available() > 0)
    if (GPSAir.encode(XBeeSerial.read()));
      gpsEncode();
      displayInfo();
}

void gpsEncode()
{
  if (GPSGround.location.isValid())
  {

    LongitudeHome = (GPSGround.location.lng());
    LatitudeHome =  (GPSGround.location.lat());
    AltitudeHome = ((GPSGround.altitude.value())/100);
  }
  if (GPSAir.location.isValid())
  {
    LongitudeCopter = (GPSAir.location.lng());
    LatitudeCopter = (GPSAir.location.lat());
    AltitudeCopter = ((GPSAir.altitude.value())/100);
  }
}

void displayInfo()
{
  Serial.print(F("Ground Location: ")); 
  if (GPSGround.location.isValid())
  {
    Serial.print(LatitudeHome);
    Serial.print(F(","));
    Serial.print(LongitudeHome);
    Serial.print(F(","));
    Serial.print(AltitudeHome);
  }
  else
  {
    Serial.print(F("INVALID"));
  }
  
    Serial.print(F("  Copter Location: ")); 
  if (GPSAir.location.isValid())
  {
    Serial.print(LatitudeCopter);
    Serial.print(F(","));
    Serial.print(LongitudeCopter);
    Serial.print(F(","));
    Serial.print(AltitudeCopter);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Ground Date/Time: "));
  if (GPSGround.date.isValid())
  {
    Serial.print(GPSGround.date.month());
    Serial.print(F("/"));
    Serial.print(GPSGround.date.day());
    Serial.print(F("/"));
    Serial.print(GPSGround.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Copter Date/Time: "));
  if (GPSAir.date.isValid())
  {
    Serial.print(GPSAir.date.month());
    Serial.print(F("/"));
    Serial.print(GPSAir.date.day());
    Serial.print(F("/"));
    Serial.print(GPSAir.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
    
    
  Serial.println();
}

When I change the types from long to float it changes a little. I need to make calculations with the coordinates.
Can someone please tell me, what I am doing wrong? The most i got until now was 47.21 / 7.31 and not something like 47.214521 / 7.316723.
Please excuse my english :slight_smile:
Thank you!
Florian

When you .print() a floating-point value the default is to print to 2 decimal places. The float/double type can hold between 6 and 7 digits so use .print(value,7) to get all of the significant digits (and then some).

Note that long has 9-10 significant digits.

Thanks for your replies. Unfortunatels I don't get it. Even if I write:

//  Serial.print(F("Ground Location: ")); 
//  if (GPSGround.location.isValid())
//  {
//    Serial.print(LatitudeHome);
//    Serial.print(F(","));
//    Serial.print(LongitudeHome);
//    Serial.print(F(","));
//    Serial.print(AltitudeHome);

It's not displayed correct.
Thank you,
Florian

eichfl:
Thanks for your replies. Unfortunatels I don't get it. Even if I write:

//  Serial.print(F("Ground Location: ")); 

//  if (GPSGround.location.isValid())
//  {
//    Serial.print(LatitudeHome);
//    Serial.print(F(","));
//    Serial.print(LongitudeHome);
//    Serial.print(F(","));
//    Serial.print(AltitudeHome);



It's not displayed correct.
Thank you,
Florian

what does it display?

Serial.print(LatitudeHome);

Serial.print(LatitudeHome , 7); assuming LatitudeHome is a float.

Your code is not handling the GPS information correctly.
Replace this:

  // This sketch displays information every time a new sentence is correctly encoded.
  while (GPSSerial.available() > 0)
    if (GPSGround.encode(GPSSerial.read()));
  while (XBeeSerial.available() > 0)
    if (GPSAir.encode(XBeeSerial.read()));
      gpsEncode();
      displayInfo();

with this:

  // This sketch displays information every time a new sentence is correctly encoded.
  while (GPSSerial.available() > 0)
    if (GPSGround.encode(GPSSerial.read())) gpsEncode();
  while (XBeeSerial.available() > 0)
    if (GPSAir.encode(XBeeSerial.read()));
  displayInfo();

You may have the same kind of error in your XBeeSerial handling but I've never used an XBee so I don't know whether or not it is correct.

Pete

Replace this:...with this:

And then think about what each while loop and if statement is ACTUALLY doing, and make the appropriate corrections.

Nice try, el_supremo, but you need to fix the underlying problems in the code as well as the poor layout.

I didn't say that would fix everything.

Pete

el_supremo:
I didn't say that would fix everything.

Pete

OK. Point taken.