I am using an Arduino Mega 2560 with GT-U7 to build a photo observation station - for rocket launches.. The launch site is fixed and known but the observation station may change with conditions. I need just location fix, not a continuous track of a moving station. I found an example code in these Fora and I want to be certain that I am applying them correctly. The names and commenting have been changed to aid my development. There are two basic questions:
1: The use of if (Serial1.available() > 0) vs. while (Serial1.available() > 0)
I assume that the "if" case applies to my fixed station and will return
1 result but the "while: case applies to a tracker and will return
repeated results as the tracker moves.
2: The use of if (gps.location.isValid()) vs. if(gps.location.isUpdated())
I assume that the "isValid" case applies to my fixed station and the
"isUpdated" case applies to a tracker.
Thanks, the two versions of the code around these options are posted below:
Amplifying my question, I simply want one location from the GT-U7. The code examples do not seem to work when placed in void setup(). The code does work when placed in void loop(). Since the operation of my project occurs 1 time, I want to have all of the code ( calculating positions, moving servos, etc, in void setup() with void loop() present but empty. In addition,, if I try to add any code around the GPS code, again, it does not work. I suspect this has something to do with the Serial1 port as the GPS unit functions.
// This code, for a fixed station displays information
// when a new sentence is correctly encoded.
// https://forum.arduino.cc/t/if-gps-encode-gpsserial-read/1023492/4
void some_function()
{ // begin some function
if (Serial1.available() > 0)
{ // begin there is a character available from the GPS at a fixed station
if (gps.encode(Serial1.read()))
{ // begin that available character completed a message
Serial.println(F(" display fixed station info "));
Z21_displayInfo();
} // end that available character completed a message
if (millis() > 5000 && gps.charsProcessed() < 10)
{ // begin nothing received
Serial.println(F("No GPS detected"));
while (true)
;
} // end nothing received
} // end there is a character available from the GPS at a fixed station
} // end some_function()
void Z21_displayInfo()
{ // begin Z21 display fixed info 1 time
Serial.println(F(" display fixed info here "));
if (gps.location.isValid())
{ // begin the completed message is valid ( fixed location ) / updated the location ( moving tracker )
double FixedLat = gps.location.lat();
double FixedLon = gps.location.lng();
double FixedAlt = gps.altitude.meters() / 1000;
Serial.println(F(" "));
Serial.print(F(" Fixed Lat is ")), Serial.println(FixedLat);
Serial.print(F(" Fixed Lon is ")), Serial.println(FixedLon);
Serial.print(F(" Fixed Alt in km is ")), Serial.println(FixedAlt);
Serial.println(F(" "));
} // end the completed message is valid ( fixed location )
else { Serial.print(F(" FIXED LOCATION INVALID")); }
Serial.println();
} // end Z21 display fixed info 1 time
// This code, for a moving tracker displays information
// when a new sentence is correctly encoded.
// https://forum.arduino.cc/t/if-gps-encode-gpsserial-read/1023492/4
void some_function()
{ // begin some function
while (Serial1.available() > 0)
{ // begin there is a character available from the GPS from a tracker
if (gps.encode(Serial1.read()))
{ // begin that available character completed a message
Serial.println(F(" display tracker info "));
Z21_displayInfo();
} // end that available character completed a message
if (millis() > 5000 && gps.charsProcessed() < 10)
{ // begin nothing received
Serial.println(F("No GPS detected"));
while (true)
;
} // end nothing received
} // end there is a character available from the GPS from a tracker
} // end some_function()
void Z21_displayInfo()
{ // begin Z21 display tracker info repeatedly
Serial.println(F(" display tracker info here "));
if (gps.location.isUpdated())
{ // begin the completed message is valid ( fixed location ) / updated the location ( moving tracker )
double TrackLat = gps.location.lat();
double TrackLon = gps.location.lng();
double TrackAlt = gps.altitude.meters() / 1000
Serial.println(F(" "));
Serial.print(F(" Tracker Lat is ")), Serial.println(TrackLat);
Serial.print(F(" Tracker Lon is ")), Serial.println(TrackLon);
Serial.print(F(" Tracker Alt in km is ")), Serial.println(TrackAlt);
Serial.println(F(" "));
} // end the completed message updated the location ( moving tracker )
else { Serial.print(F(" TRACKER LOCATION NOT UPDATED ")); }
Serial.println();
} // end Z21 display tracker info repeatedly