I have written the following code. I want the string returned from the method
displayInfo( )
to be updated and printed just once, but the method sends strings repeatedly. If I copy the same code in void setup( ) function it is not printing any value.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS
SoftwareSerial ss(RXPin, TXPin);
String msg="";
String message="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
message = displayInfo();
Serial.print(message);
}
String displayInfo()
{
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String logi=String(gps.location.lng(),3);
msg=lati+","+logi+"\n";
return(msg);
}
}
I have updated the code to recover some of the errors like function returning value with a global variable but it still does not provide me a single String value even after I had put the void loop( ) code in void setup( )
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
String msg="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
while (ss.available()>0)
if (gps.encode(ss.read()))
displayInfo();
Serial.print(msg);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
}
void displayInfo()
{
//Serial.print(F("Location: "));
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String longi=String(gps.location.lng(), 3);
msg="location: "+lati+","+longi+"\n";
}
else
{
msg=msg+"invalid";
}
}