GPS code works in loop but not in setup?

So I got a Neo-6M GPS module. My code works fine but I want that loop function is emtpy because I want to use this code further for another project. So as you would without a great knowledge, I copy pasted the code from the loop into setup. And now nothing works. Do you have a suggestion? Thanks in advance.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

int RXPin = 16;
int TXPin = 17;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();

  // If 5000 milliseconds pass and there are no characters coming in
  // over the software serial port, show a "No GPS detected" error
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while (true);
  }
}

void displayInfo()
{
  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat());
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng());
  }
  else
  {
    Serial.println("Location: Not Available");
  }

  Serial.println();
  delay(1000);
}

Please post the sketch with the GPS code in setup()

I assume that you realise that the code in setup() runs just once. If you want to run more than one thing at a time then you are going about it the wrong way

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

int RXPin = 16;
int TXPin = 17;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();

  // If 5000 milliseconds pass and there are no characters coming in
  // over the software serial port, show a "No GPS detected" error
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while (true);
  }

}

void loop()
{
  
}

void displayInfo()
{
  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat());
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng());
  }
  else
  {
    Serial.println("Location: Not Available");
  }

  Serial.println();
  delay(1000);
}

I know. Just one command that the gps data should be read and done.

But if nothing is instantly available you just whistle right past the while loop.

Do something to keep looking for the chars from you GPS for 5 seconds or however long it might take to get talking.

It looks like some braces plopped in there just right might do that trick...

a7

the NMEA sentence might not be fully in the incoming buffer, so if you read only part of it (the while will empty the buffer pretty fast compared to the 9600 bauds communication link) then you won't get a fix so you won't call displayInfo().

something like this in your setup()

do {
  if (gpsSerial.available() != 0)
    if (gps.encode(gpsSerial.read()))  {
      displayInfo();
      break;
    }
} while (millis() < 10000);

would wait for up to 10 seconds after the start of your arduino sketch to get a full NMEA sentence and call displayInfo();

Which data items do you want to get from the GPS in setup()

If you really do only want to read the GPS once then it would be easier to do it in loop() and once it had been read don't read it again

I would not call that easier. it will cost you a boolean (gee 1 byte and a test at every loop! :wink: ) to not do it again whereas things that need to be done only once really belong to setup(). A while loop as proposed above would do it. If it's necessary to wait for a fix before doing anything else then change 10000 into true.

I would say it is better to have that in the loop though, if you want to do something else whilst you wait for the first fix.

Here you go:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

int RXPin = 16;
int TXPin = 17;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

  while (1)
  {
    // This sketch displays information every time a new sentence is correctly encoded.
    while (gpsSerial.available() > 0)
      if (gps.encode(gpsSerial.read()))
        displayInfo();

    // If 5000 milliseconds pass and there are no characters coming in
    // over the software serial port, show a "No GPS detected" error
    if (millis() > 5000 && gps.charsProcessed() < 10)
    {
      Serial.println("No GPS detected");
      while (true);
    }
  }
}

void displayInfo()
{
  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat());
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng());
  }
  else
  {
    Serial.println("Location: Not Available");
  }

  Serial.println();
  delay(1000);
}

void loop() {}

Your suggestion worked for the time I had to test it. Currently I'm unable to test but if I have the time I'll report back to you.

Be aware that most of the time when gps.encode() returns true (and then displayInfo() is called) there will be no position updates. There is then little point in displaying the old unchanged postion information again.

Also that delay(1000) in displayInfo() can cause the TinyGPS++ library to miss the decode of the two sentences with location information, $GPGGA and $GPRMC.

LOL, that’s even better than empty.

a7

Thanks for the solution for my problem.

Be aware that John's sketch does exactly what you specified but as written the loop() function is never executed so any code in it will never run