You are calling delay(), you are thus losing nearly all the output from
your GPS. Remove that call. Look at blinkWithoutDelay example if you want
to limit changes to the tone's frequency to regular intervals without stalling the
whole Arduino.
In addition, the encode() method returns true (the last character completed a sentence) or false (the data the instance has now is incomplete). It makes no sense to call lat() and lng() when encode() has returned false.
MarkT:
You are calling delay(), you are thus losing nearly all the output from
your GPS. Remove that call. Look at blinkWithoutDelay example if you want
to limit changes to the tone's frequency to regular intervals without stalling the
whole Arduino.
Hi MarkT
Thanks for the reply. I will check out the 'Blink Without Delay' example.
PaulS:
In addition, the encode() method returns true (the last character completed a sentence) or false (the data the instance has now is incomplete). It makes no sense to call lat() and lng() when encode() has returned false.
Hi PaulS
Sorry to take so long to reply. I've been trying to get my head around your suggestion. Am I correct in thinking that I should change the following;
while(ss.available() > 0)
gps.encode(ss.read());
to
while (ss.available() > 0)
if (gps.encode(ss.read()))
Specifically, the line 'gps.encode(ss.read())' performs two functions?
Am I correct in thinking that I should change the following;
Yes.
It encodes and stores the NMEA sentence.
I'm not sure why Mikal Hart chose the name encode() for the function, since it really isn't encoding anything. What it does is append the character passed in to the array of characters, and checks to see if that character represents the end of a sentence.
Am I correct in thinking that I should change the following;
Yes.
It encodes and stores the NMEA sentence.
I'm not sure why Mikal Hart chose the name encode() for the function, since it really isn't encoding anything. What it does is append the character passed in to the array of characters, and checks to see if that character represents the end of a sentence.
Hi PaulS
I was wondering if you could guide me a little with my setup routine? I've currently got the following.
void setup(){
ss.begin(38400); //SPEED OF MY GPS
while(!gps.location.isValid()) //CHECK THAT NO GPS LOCK IS TRUE
while(ss.available() > 0) //GET DATA FROM SERIAL PORT IF AVAILABLE
if(gps.encode(Serial.read())){ //CHECK WE HAVE A COMPLETE SENTENCE
Serial.println("NOT VALID"); //PRINT NOT VALID
delay(1000); //WAIT A SECOND BEFORE TRYING AGAIN
} //WE NOW HAVE GPS LOCK
homeLat = gps.location.lat(); //RECORD LAT
homeLng = gps.location.lng(); //RECORD LON
Serial.print(homeLat, 6); //PRINT LAT
Serial.print(", "); //PRINT ,
Serial.println(homeLng, 6); //PRINT LNG
}
Although the above appears to work, I just want to make sure that my lat and lng are correct before proceeding to the main loop. Really appreciate the help.
I have real problems reading your code. Every while statement should have { and }. Every { goes on a new line, in my opinion.
Comments do not, in my opinion, go on the line with the code. If there is a need to document something with comments, the comments are important enough to precede the code.
while(ss.available() > 0) //GET DATA FROM SERIAL PORT IF AVAILABLE
if(gps.encode(Serial.read())){ //CHECK WE HAVE A COMPLETE SENTENCE
Serial.println("NOT VALID"); //PRINT NOT VALID
delay(1000); //WAIT A SECOND BEFORE TRYING AGAIN
} //WE NOW HAVE GPS LOCK
If the character that was read completes a sentence, encode returns true, so you print NOT VALID. I can't imagine why.
If the character completes a sentence, then is the time to check that the sentence included enough data to be a valid location. If that's the case, you should break out of the while data is available loop, right away, not a second later. If the character does not complete a sentence, you most certainly do NOT want to wait a second to read another character.
Therefore, that delay() is completely misguided.
Although the above appears to work
For some definition of work, I guess.
I just want to make sure that my lat and lng are correct before proceeding to the main loop.
There may be no data available immediately after calling ss.begin(). It may (probably is) necessary to delay() a bit to give the GPS time to wake up and start sending data.
I want to wait until I have valid GPS coordinates before proceeding. The following line should ensure my sketch keeps running indefinitely as the software serial buffer will be flooded with NMEA sentences.
while(ss.available() > 0)
The only way out from my understanding is if the following line returns TRUE.
if(gps.location.isValid())
Then the following line get us out of the while loop.
while(ss.available() > 0)
{
if(gps.encode(ss.read()))
{
Serial.println("Sentence complete");
// Add more print statements here to dump the GPS data
// The class has several. gps.charsProcessed(), sentencesWithFix(),
// failedChecksum() and/or passedChecksum()
if(gps.location.isValid())
{
What do you see then?
Personally, I think that the class could use a method to dump the data in the private field named term to some output Stream, like Serial, to make debugging easier.
Thanks for getting back to me. I'm not quite sure what you would like me to do? I've written the following which works. I must admit I do find the number of conditionals confusing. Specifically where each one exits. I really appreciate your perseverance.
I'm not quite sure what you would like me to do? I've written the following which works.
Well, then, nothing. You've determined for yourself what the problem was. I was just suggesting more debug output to lead you to see the problem, and the solution. But, you got there already, and hopefully you understand why the works, and why that order is necessary.
To be honest I'm still not happy with what I've written. There must be a cleaner way. Just thinking out loud.
It's good. If you want to stay in setup until you get a valid location, you need to first determine if you have a valid location. If not, you spin reading any available data. Obviously this while loop will be execute many times, before the complete sentence arrives. On any given execution, the loop will probably only iterate a time or two. Serial data can be read from the buffer far faster than it can be received and put in the buffer. Just like you can read far faster than I can type.
You still should have a break; statement after setting the home location, so that the code doesn't start reading the next sentence, if there is data pending, before the outer while discovers that the location is valid.