Post the code that doesn’t run in full and post any error compiler generates. There is nothing wrong with calling a function from the loop, you must have done something else you are not telling
Ok, Bizarre:
If I now call "getGPS()" from within the loop, I get to "void getGPS()" and all is now OK.
However, I need to call "getGPS()" once from setup ad this does not work.
I do get to "getGPS()" and Serialprint shows "Got to getGPS".
Complete code:
#include <SoftwareSerial.h>
#include <NMEAGPS.h>
SoftwareSerial gpsSerial(5, 4); // RX, TX
//#include <GPSport.h>
//------------------------------------------------------------
// Check that the config files are set up properly
#if !defined( NMEAGPS_PARSE_RMC ) & \
!defined( NMEAGPS_PARSE_GGA ) & \
!defined( NMEAGPS_PARSE_GLL )
#error You must uncomment at least one of NMEAGPS_PARSE_RMC, NMEAGPS_PARSE_GGA or NMEAGPS_PARSE_GLL in NMEAGPS_cfg.h!
#endif
#if !defined( GPS_FIX_LOCATION )
#error You must uncomment GPS_FIX_LOCATION in GPSfix_cfg.h!
#endif
NMEAGPS gps;
// The base location, in degrees * 10,000,000 (-25.857210, 28.187360)
NeoGPS::Location_t myMall( -258572100L, 281873600L ); // Centurion Mall
// The base location, in degrees * 10,000,000 (-25.856182, 28.179309)
NeoGPS::Location_t myHome( -258561820L, 281793090L ); // Home
// The base location, in degrees * 10,000,000 (-25.855251, 28.180597)
NeoGPS::Location_t myStreet( -258552510L, 281805970L ); // Road South Street
char latHex[9];
char lonHex[10];
void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600);
getGPS();// This gets to "getGPS", but code gives no output
}
void loop()
{
// getGPS();// This works and calls getGPS and output is all good
}
void getGPS()
{
Serial.println("Got to getGPS");
while (gps.available( gpsSerial )) {
gps_fix fix = gps.read(); // save the latest
// GET LATITUDE
long myLat = abs(fix.latitudeL());
char latHex[9];
sprintf(latHex, "%08lX", myLat);
Serial.print("Lat HEX: ");
Serial.println(latHex);
// GET LONGITUDE
long myLon = abs(fix.longitudeL());
char lonHex[9];
sprintf(lonHex, "%08lX", myLon);
Serial.print("Lon HEX: ");
Serial.println(lonHex);
// When we have a location, calculate how far away we are from the base location.
if (fix.valid.location) {
float distHome = fix.location.DistanceKm( myHome );
distHome = (distHome * 1000);
Serial.print( F("Distance Home: ") );
Serial.print( distHome );
Serial.println( F(" Meters") );
float distStreet = fix.location.DistanceKm( myStreet );
distStreet = (distStreet * 1000);
Serial.print( F("Distance Street: ") );
Serial.print( distStreet );
Serial.println( F(" Meters") );
float distMall = fix.location.DistanceKm( myMall );
distMall = (distMall * 1000);
Serial.print( F("Distance Mall: ") );
Serial.print( distMall );
Serial.println( F(" Meters") );
Serial.println("--------------------------------");
delay(20);
Serial.println();
} else
// Waiting...
Serial.print( '.' );
}
} // End loop
Log with call getGPS(); from setup:
Got to getGPS
Log with call getGPS(); from loop:
Got to getGPS
Got to getGPS
Got to getGPS
Got to getGPS
Got to getGPS
Got to getGPS
Lat HEX: 0F6959E5
Lon HEX: 10CBCFF4
Distance Home: 9.88 Meters
Distance Street: 174.90 Meters
Distance Mall: 818.45 Meters
--------------------------------
I have confirmed that in both instances, the GPS module (Quectel L80) is running.
Did you try to see what's going on with the thing?
void getGPS()
{
Serial.print( " is gpsavailable? "); //I mean we can't think of everything for you
Serial.print( gps.available( gpsSerial ) );//apply yourself.
Serial.println();
while (gps.available( gpsSerial )) {
gps_fix fix = gps.read(); // save the latest
because doesn't it make sense that if gps.available( gpsSerial ) equals a 0 then the data is not ready for you to get and print?
And dude, if its printing "Got to getGPS", what does that mean?