When I want to read the number of satellites by:
gps.satellites.value()
I had to change the code... Why?
Post the complete code. Otherwise, we're just speculating.
I can tell you that, from the Arduino's perspective in loop()
, the GPS characters are coming in veeeeerrrrry slooooowwwwly. Thousands of iterations happen between each character. Eventually, the last character of one GPS sentence finally comes in, and gps.encode
finally returns true
.
So this is not the correct way to use TinyGPSPlus:
while (ss.available()){
gps.encode(ss.read());
gpsUpdated = true;
}
if (gpsUpdated==true){
displayInfo();
gpsUpdated = false;
}
Every time you receive one character, you set gpsUpdated = true
, and that's not true. It may have just received the 3rd character of the latitude, so it doesn't even have the whole value yet.
Normally, ss.available()
is a small number, like 1. It can never be bigger than 64. If the Arduino has been busy doing something else (like printing in displayInfo
), serial characters get stored in the input buffer until the code gets back to the ss.available()
statement. In fact, you can spend too much time elsewhere (printing >:( ), and the input buffer overflows. The 65th character and everything after is lost. That sentence from the GPS device will also be lost, so the lat, lon, satellite count, etc. from the sentence will also be lost. No fields will be updated from that sentence. Bad juju. See here for some pretty pics describing the printing problem.
As to why the original code doesn't work, I can make up two reasons without seeing the code:
1) When the last character for a sentence finally comes in, encode
returns true
. At this point the TinyGPSPlus example thoughtfully displays everything. -_- However, during each 1-second interval, 5 or more sentences are being sent by the GPS device. Only one of those sentences will have the satellites
number in it (refer to this table do see which sentences have which pieces). So only one out of 5 calls to displayInfo
will actually have an updated satellites.value()
.
2) TinyGPSPlus uses an "updated" flag to track which pieces change. But the instant you ask for the value, that flag is cleared. For example, the following would not work:
if (gps.altitude.valid()) {
Serial.print( gps.altitude.value() );
if (gps.altitude.updated) {
// we moved! [never happens]
stabilize_vertical_motion();
}
}
Printing the value clears the flag, and the next if
condition is never true. Just a guess.
Cheers,
/dev