Calling a function from loop

I have the following code within the "void loop()"

void loop()
{
  while (gps.available( gpsSerial )) {
    gps_fix fix = gps.read();
    
    // GET LATITUDE
    String latTx = (String(fix.latitudeL()));
    latTx = latTx.substring(1, 9);
    long myLat = latTx.toInt();
    sprintf(latHex, "%08lX", myLat);
    Serial.print("Lat HEX: ");
    Serial.println(latHex);

    // GET LONGITUDE
    String lonTx = (String(fix.longitudeL()));
    lonTx = lonTx.substring(0, 8);
    long myLon = lonTx.toInt();
    sprintf(lonHex, "%08lX", myLon);
    Serial.print("Lon HEX: ");
    Serial.println(lonHex);
  }
}

This works great.

However, I need to call the following function from within the "void loop()"

void getGPS()
{
  while (gps.available( gpsSerial )) {
    gps_fix fix = gps.read();
    
    // GET LATITUDE
    String latTx = (String(fix.latitudeL()));
    latTx = latTx.substring(1, 9);
    long myLat = latTx.toInt();
    sprintf(latHex, "%08lX", myLat);
    Serial.print("Lat HEX: ");
    Serial.println(latHex);

    // GET LONGITUDE
    String lonTx = (String(fix.longitudeL()));
    lonTx = lonTx.substring(0, 8);
    long myLon = lonTx.toInt();
    sprintf(lonHex, "%08lX", myLon);
    Serial.print("Lon HEX: ");
    Serial.println(lonHex);
  }
}

With

void loop()
{
  getGPS();
}

For some reason I cannot call the "getGPS()" function as it will not run.
Thee code only runs in the "void loop()"

Please show the code where you calling function from loop

do you mean it won't run or that it doesn't recognize any input from gps?

go back to basics and put a print at the top of getGPS() to see that it is being called repeatedly

you could add a call to "gps.available( gpsSerial)" in loop() is see if it's only recognized in loop and not in getGPS()

does "gps.available( gpsSerial)" really need an argument, "gpsSerial"?

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

this code change, does it print?

void getGPS()
{
Serial.print( "i'm doing the getGPS thing");
Serial.println();
  while (gps.available( gpsSerial )) {
    gps_fix fix = gps.read();
    
    // GET LATITUDE
    String latTx = (String(fix.latitudeL()));
    latTx = latTx.substring(1, 9);
    long myLat = latTx.toInt();
    sprintf(latHex, "%08lX", myLat);
    Serial.print("Lat HEX: ");
    Serial.println(latHex);

    // GET LONGITUDE
    String lonTx = (String(fix.longitudeL()));
    lonTx = lonTx.substring(0, 8);
    long myLon = lonTx.toInt();
    sprintf(lonHex, "%08lX", myLon);
    Serial.print("Lon HEX: ");
    Serial.println(lonHex);
  }
}

Sounds like there must be a mistake elsewhere in the sketch. Please show the whole sketch.

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?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.