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.
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();
I would not call that easier. it will cost you a boolean (gee 1 byte and a test at every loop! ) 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.
#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() {}
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.