Help with fetch GPS coordinate (call variable from other function)

Hello forum members
i Need help with fecth gps cordinate 1 time when i power on gps device and hold them in variable as TAKEOFF coordinate
float TakeoffLO = flon;
float TakeoffLA = flat;

Then GPSwill measure relative distance from them.
Now i use hard code coordinate .
i think i need to pass the variable from 1 function to other
i have a little experience with programming ,
Can any advice please
Any advice will be much appreciate
Thanks .

#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(4, 3);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void Takeoff0()
{
float flat , flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat ,&flon, &age) ;
float TakeoffLA1 = flat;
float TakeoffLO1 = flon;

}

void setup()
{
Serial.begin(115200);
lcd.begin(20,4);

ss.begin(9600);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
delay(1000);
}

void loop()
{
float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
// static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

//print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
//print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
gps.f_get_position(&flat, &flon, &age);
print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
float GPSSPEED = gps.f_speed_kmph() ;

// **********************************************************************************
// HARD CODE GPS CORDINATE
//(NEED TO USE DYNAMIC VARIABLE THAT WILL FETCH CURRENT GPS LOCATION START POINT)
//float TakeoffLO ;
float TakeoffLO = (35.217018);
float TakeoffLA = (31.771959);
//************************************************************************************

float COURSE = gps.f_course();
float GPSALT = gps.f_altitude() ;
float GPSALT1 = round(GPSALT*10)/1000;
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Distance = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);

Hello
i am trying to resolve problem and i am newbie to programing
i am trying to move variable GPS COORDINATE from function void Takeoff0() to void loop()
can anyone help with this problem
any advice will be much appreciate
Thanks
Itay

void Takeoff0()
{
float flat , flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat ,&flon, &age) ;
float TakeoffLA1 = flat;
float TakeoffLO1 = flon;

}

void setup()
{
Serial.begin(115200);
lcd.begin(20,4);

ss.begin(9600);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
delay(1000);
}

void loop()
{
float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
// static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

//print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
//print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
gps.f_get_position(&flat, &flon, &age);
print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
float GPSSPEED = gps.f_speed_kmph() ;

float GPSLAT =TakeoffLA1;
float GPSLON =TakeoffLO1;

Make TakeOffLA1 and TakeOffLO1 global variables. Or build a struct to hold two floats and return it from the function.

float TakeoffLA1 = flat;
float TakeoffLO1 = flon;

These variables are local to Takeoff0(). They will go out of scope when the function ends. They seem pointless. If you want to be able to use them elsewhere, they must be global.

I can't see where you are reading the GPS, storing the data, deciding that there is data to extract from the sentence, or calling Takeoff0().

Didn't I just answer this exact same question? OP, please don't cross post the same question in multiple places. It wastes our time.

IN the same section of the forum no less.

http://forum.arduino.cc/index.php?topic=360922.0

Hi this variables are useless because they are in the Takeoff scop and not pass to the loop function i understand that they must be global ! i placed them in function because i get Error outside a function when trying to read the gps data outside a function to get global variable this the the sentence that pull the GPS data

gps.f_get_position(&flat ,&flon, &age) ;

void Takeoff0() { float flat , flon; unsigned long age, date, time, chars = 0; unsigned short sentences = 0, failed = 0; gps.f_get_position(&flat ,&flon, &age) ; static float TakeoffLA1 = flat; static float TakeoffLO1 = flon;

}

Right, you can't read the GPS outside of the function. Declare the variables at global scope and set them to 0, and then give them values in this function.

ok ,
i need to get variable i get in take of function to the variables i declare as global
Can you send me an example of code i can copy and change ?
Thanks for the help , many appreciate

#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h>

float TakeoffLA;
float TakeoffLO;

/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(4, 3);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void Takeoff()
{
float flat , flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat ,&flon, &age) ;
float TakeoffLA = flat;
float TakeoffLO = flon;

}

Please note my use of code tags and start using them when you post code.

#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h> 


 float TakeoffLA;
 float TakeoffLO;
 
/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(4, 3);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void Takeoff()
{
float flat , flon; 
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  gps.f_get_position(&flat ,&flon,  &age) ;
 TakeoffLA = flat;
 TakeoffLO = flon;

}

Taking the “float” off the variables in the function and now the values are put into the global variables declared up above instead of creating new float variables with the same names that immediately go out of scope.

You know you get more accuracy with lat and lon if you use fixed point math and unsigned long. 9 digits of precision with fixed point vs 6 or maybe 7 with float.

Thanks for code example
The problem now is that variable dont hold or get the GPS cordinate
i print the variable to the screen and i get 0 for both : Latitude and Longitude

i attached prn of output results
Any idea ?
Tx
Itay

gps-data.png

itaym: Thanks for code example The problem now is that variable dont hold or get the GPS cordinate i print the variable to the screen and i get 0 for both : Latitude and Longitude

Did you print before or after you called that code to give them a value?

It's time to show the whole code. Your snippet is useless to diagnose your current issue. Show the WHOLE thing. And use CODE TAGS so we can easily copy the code. If you don't know what that means, read the "How To Use This Forum" thread at the top of the board.

Well i try to make the variables in the Takeofflocation function static but it don’t work
They are getting update all the time /get the same coordinateas the gps moving
So the distance from takeoff location always 0 also bearing
i also attached a picture of the lcd screen for the device i want to build (for aviation)
Thanks and many appreciate
in the end i hope the device will work correctly and i will upload it to the O.S community
With pictures and instruction so the setup will be almost PnP
Thanks
Itay

#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h>  

static float  TakeoffLA ; 
static float  TakeoffLO ;
static const int RXPin = 4, TXPin = 3;

/* This sample code demonstrates the normal use of a TinyGPS object.
  It requires the use of SoftwareSerial, and assumes that you have a
  4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(RXPin, TXPin);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void Takeofflocation()
{
float flat , flon;  
 unsigned long age, date, time, chars = 0;
 unsigned short sentences = 0, failed = 0;
 gps.f_get_position(&flat ,&flon,  &age) ;
// TakeoffLA = flat;
// TakeoffLO = flon;

}


void setup()
{
 Serial.begin(115200);
 lcd.begin(20,4);


 ss.begin(9600);
  for(int i = 0; i< 3; i++)
 {

   lcd.backlight();
   delay(250);
   lcd.noBacklight();
   delay(250);
 }
 lcd.backlight(); // finish with backlight on  
  delay(1000);
}

void loop()
{
 float flat, flon;
 unsigned long age, date, time, chars = 0;
 unsigned short sentences = 0, failed = 0;
 gps.f_get_position(&flat, &flon, &age);
 print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
 print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
float  GPSSPEED = gps.f_speed_kmph() ;
TakeoffLA = flat;
TakeoffLO = flon;

float COURSE = gps.f_course();
float   GPSALT = gps.f_altitude() ;
float GPSALT1 = round(GPSALT*10)/1000;
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Distance = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);

 gps.stats(&chars, &sentences, &failed);
 Serial.println();
 Serial.println("GPS-GROUND SPEED IS :");
 Serial.println(GPSSPEED);
 Serial.println("GPS Takeofflocation LAT");
 Serial.println(TakeoffLA);
 Serial.println("GPS Takeofflocation LON");
 Serial.println(TakeoffLO);
 Serial.println("GPS CORDINATE LAT NOW");
 Serial.println(flat);
 Serial.println("GPS CORDINATE LON NOW");
 Serial.println(flon);
 Serial.println("Distance from takeoff");


if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
// lcd.print("N/A");
}
else
{
 unsigned long distance = TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000;
 if (distance!= 0xFFFFFFFF)
 {
   
    lcd.setCursor(10,3);
// lcd.print(distance-13214);
 lcd.print(distance);
 print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
 }
 else
 {


   lcd.print(F("Invalid distance"));
 }
}


//float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);

if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
 lcd.setCursor(13,1); 
// lcd.print("N/A");
}

else
{
 unsigned long course_to1 = TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO);
 if (1!= 2)
 
 lcd.setCursor(9,1);
 lcd.print(course_to1);

 }

  
//print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
   Serial.println(" ");
   Serial.println("Bearing");
    print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
 Serial.println(" ");
 Serial.println("bearing from variable same lcd");

 Serial.println( Bearing);
 smartdelay(1000);
 lcd.setCursor(0,0);
 lcd.print("Speed");
 lcd.setCursor(9,0);
 lcd.print(GPSSPEED);
 lcd.setCursor(17,0);
 lcd.print("Kmh");
 lcd.setCursor(0,2);
 lcd.print("Altitude");
 lcd.setCursor(10,2);
 lcd.print(GPSALT);
 lcd.setCursor(13,2);
 lcd.print("  Meter");
 lcd.setCursor(0,1);
 // lcd.print("Course");
// Serial.println(gps.altitude.meters())
lcd.print("Bearing");
// lcd.setCursor(10,1);
 lcd.setCursor(12,1);
 lcd.print((char)223);
 lcd.setCursor(14,1);
 lcd.print("H");
 lcd.setCursor(16,1);
 lcd.print(COURSE);
 lcd.setCursor(19,1);
 lcd.print((char)223);
 lcd.setCursor(0,3);
 lcd.print("Takeoff");
 lcd.setCursor(18,3);
 lcd.print("Km");

}

static void smartdelay(unsigned long ms)
{
 unsigned long start = millis();
 do 
 {
   while (ss.available())
     gps.encode(ss.read());
 } while (millis() - start < ms);
}

static void print_float(float val, float invalid, int len, int prec)
{
 if (val == invalid)
 {
   while (len-- > 1)
     Serial.print('*');
   Serial.print(' ');
 }
 else
 {
   Serial.print(val, prec);
   int vi = abs((int)val);
   int flen = prec + (val < 0.0 ? 2 : 1); // . and -
   flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
   for (int i=flen; i<len; ++i)
     Serial.print(' ');
 }
 smartdelay(0);
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
 char sz[32];
 if (val == invalid)
   strcpy(sz, "*******");
 else
   sprintf(sz, "%ld", val);
 sz[len] = 0;
 for (int i=strlen(sz); i<len; ++i)
   sz[i] = ' ';
 if (len > 0) 
   sz[len-1] = ' ';
 Serial.print(sz);
 smartdelay(0);
}
static void print_date(TinyGPS &gps)
{
 int year;
 byte month, day, hour, minute, second, hundredths;
 unsigned long age;
 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
 if (age == TinyGPS::GPS_INVALID_AGE)
   Serial.print("********** ******** ");
 {
   char sz[32];
   sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
       month, day, year, hour, minute, second);
   Serial.print(sz);
 }
 print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
 smartdelay(0);
}

static void print_str(const char *str, int len)
{
 int slen = strlen(str);
 for (int i=0; i<len; ++i)
   Serial.print(i<slen ? str[i] : ' ');
 smartdelay(0);

}

Yeah, you put the code to get values for those in loop, so they'll be recalculated each time loop runs. You need to calculate those values once when you take off and then leave them alone. The static isn't doing anything, get rid of that.

Use a global variable so they will be accessible from anywhere. When you take off, calculate your coordinates and put them in those global variables. Uncomment the lines in Takeofflocation that set the values of those variables. In your code, when you take off call Takeofflocation, that will put the values of lat and lon at takeoff in those variables. Don't call Takeofflocation again unless you want new values for those variables. In the rest of your code when you want to know where you took off from, look at those two variables and they will still have the values you gathered at takeoff.

Perhaps a quick google search for "C++ variable scope" will help you to understand.

ok , thanks I understand The code don't call Takeofflocation function Any example how i can "call" a function from other function I also tried to move the commands under setup but it useless don't work Regards Itay

Any example how i can "call" a function from other function

You know hot to call digitalRead() from loop(), don't you? Why would calling your function be any different?

Hi I need to call it just one time (on startup) to get take off position and start measure distance from this point Can do it from loop with digit read Can you give example pleas ? Regards Itay

What you seem to be trying to do is completely pointless. Your Takeofflocation() function stupidly assumes that there IS GPS data to be parsed. There is NO proof that that is the case.

Your entire program needs to be restructured. The FIRST thing to do is to read data, if any, from the GPS, setting some flag to indicate when a COMPLETE sentence has been received. Only when that flag is true should you try to use the GPS data.

Setting a flag to indicate whether or not a packet is the first packet, or not, seems very simple. Saving the GPS position data when that flag is true seems incredibly simple.

I agree ,
It look like The function run before the gps even have data , and i get 1000 number .
i need a condition that check that data arrived \ receive to arduino and then parse it .
Unfortunately i have minimal programming skills , so i can read and understand code
But cannot program and write code (only basic simple function).
i use examples and fits them to my needs
Thanks for any help with the code to anyone can assist !!!

Attachment of device i am trying to build and when ready and work correctly i will upload to the internet community full code with instruction , Maybe some people will find it use full same me for aviation

#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h>  

static const int RXPin = 4, TXPin = 3;
 
/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(RXPin, TXPin);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
float  TakeoffLA ; 
float  TakeoffLO ;


void Takeofflocation()
{


}

 
void setup()
{
  Serial.begin(115200);
  lcd.begin(20,4);


  ss.begin(9600);
   for(int i = 0; i< 3; i++)
  {

    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on  
   delay(1000);
   float flat , flon;  
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  gps.f_get_position(&flat ,&flon,  &age) ;
//  delay(1000);
 
TakeoffLA = flat;
TakeoffLO = flon;
}

void loop()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  gps.f_get_position(&flat, &flon, &age);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
 float  GPSSPEED = gps.f_speed_kmph() ;
//TakeoffLA = flat;
//TakeoffLO = flon;

float COURSE = gps.f_course();
float   GPSALT = gps.f_altitude() ;
float GPSALT1 = round(GPSALT*10)/1000;
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Distance = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);

  gps.stats(&chars, &sentences, &failed);
  Serial.println();
  Serial.println("GPS-GROUND SPEED IS :");
  Serial.println(GPSSPEED);
  Serial.println("GPS Takeofflocation LAT");
  Serial.println(TakeoffLA);
  Serial.println("GPS Takeofflocation LON");
  Serial.println(TakeoffLO);
  Serial.println("GPS CORDINATE LAT NOW");
  Serial.println(flat);
  Serial.println("GPS CORDINATE LON NOW");
  Serial.println(flon);
  Serial.println("Distance from takeoff");


if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
// lcd.print("N/A");
}
else
{
  unsigned long distance = TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000;
  if (distance!= 0xFFFFFFFF)
  {
    
     lcd.setCursor(10,3);
 // lcd.print(distance-13214);
  lcd.print(distance);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
  }
  else
  {

 
    lcd.print(F("Invalid distance"));
  }
}


//float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);

if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
  lcd.setCursor(13,1); 
// lcd.print("N/A");
}

else
{
  unsigned long course_to1 = TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO);
  if (1!= 2)
  
  lcd.setCursor(9,1);
  lcd.print(course_to1);

  }

   
//print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
    Serial.println(" ");
    Serial.println("Bearing");
     print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA,  TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  Serial.println(" ");
  Serial.println("bearing from variable same lcd");

  Serial.println( Bearing);
  smartdelay(1000);
  lcd.setCursor(0,0);
  lcd.print("Speed");
  lcd.setCursor(9,0);
  lcd.print(GPSSPEED);
  lcd.setCursor(17,0);
  lcd.print("Kmh");
  lcd.setCursor(0,2);
  lcd.print("Altitude");
  lcd.setCursor(10,2);
  lcd.print(GPSALT);
  lcd.setCursor(13,2);
  lcd.print("  Meter");
  lcd.setCursor(0,1);
  // lcd.print("Course");
 // Serial.println(gps.altitude.meters())
 lcd.print("Bearing");
 // lcd.setCursor(10,1);
  lcd.setCursor(12,1);
  lcd.print((char)223);
  lcd.setCursor(14,1);
  lcd.print("H");
  lcd.setCursor(16,1);
  lcd.print(COURSE);
  lcd.setCursor(19,1);
  lcd.print((char)223);
  lcd.setCursor(0,3);
  lcd.print("Takeoff");
  lcd.setCursor(18,3);
  lcd.print("Km");
 
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartdelay(0);
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartdelay(0);
}
static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);

}