1
votes

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";
  }
  }
1

1 Answers

0
votes

EDIT: The ss (SoftwareSerial) has a buffer wich contains the data that is ready to be send. ss.begin() will return 0 in your setup since the buffer is still empty, therefore the while loop will not be iterated even once.

The loop() function of the arduino works like a while so by placing the content of that while loop, and replacing the while with an if, you will be able to keep testing until you have a message in the buffer.

By adding a boolean to check if you have already sent a message, you can make sure only 1 message will be sent.

  #include <TinyGPS++.h>
  #include <SoftwareSerial.h>
  static const int RXPin = 12, TXPin = 13;
  static const uint32_t GPSBaud = 9600;

  TinyGPSPlus gps;

  SoftwareSerial ss(RXPin, TXPin);

  boolean sent = false;

  void setup()
  {
   Serial.begin(9600);
   ss.begin(GPSBaud);   
  }

  void loop()
  {
   if (ss.available()>0 && sent == false){
    if (gps.encode(ss.read())){
     String msg = displayInfo();
     if (msg != NULL){
      Serial.print(msg);
      sent = true;
     }
    }
   }
  }

  String displayInfo()
  {
   if (gps.location.isValid())
   {
    String msg="";
    String lati=String(gps.location.lat(), 3);
    String logi=String(gps.location.lng(),3);
    msg=lati+","+logi+"\n";
    return(msg);
   }
   else{
     return NULL;
   }
  }

By returning a NULL in the displayinfo()'s else statement, and testing for it within the loop(), you can ensure that you will only print a message when everything was working.