Hi I am trying to make search GPS (using tinGPSplus) every x time. The problem is when I do compile the code:
void loop()
{
read=BP_mnger.pressedButton();
navigateMenus(read);
dealWithUsedEvents(getmenuUsed());
setmenuUsed(0);
if (isStarted && (GPSSearchPeriod<=millis()-oldMillisVallue)) {
oldMillisVallue=millis();// if i comment this line it works
Serial.print(F("oldMillisVallue: "));
Serial.println(oldMillisVallue);
while (ss.available() > 0){
if (gps.encode(ss.read()))
if (gps.location.isUpdated() || gps.date.isUpdated() || gps.time.isUpdated() || gps.altitude.isUpdated() || gps.satellites.isUpdated() || gps.hdop.isUpdated())
GPSSentence=makeGPSSentence( );
}
}
I get this:
oldMillisVallue: 22355
oldMillisVallue: 24356
oldMillisVallue: 26357
oldMillisVallue: 28358
oldMillisVallue: 30359
When I comment out:
oldMillisVallue=millis();// if i comment this line it works
I get this :
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
2000,0,0,4294967295,15,43,3,36,5,41,4.066298,48.269157,77,0.12,90,380,98,2,1,3
2000,0,0,4294967295,15,43,3,39,5,43,4.066298,48.269157,79,0.122200,92,380,98,2,1,3
------------------------
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
2000,0,0,4294967295,15,43,4,39,5,43,4.066265,48.269126,80,0.12,94,380,101,3,2,5
2000,0,0,4294967295,15,43,4,41,5,45,4.066265,48.269127,82,0.121900,94,380,103,3,2,5
------------------------
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
oldMillisVallue: 0
1 Answer 1
You're only reading from the serial port every GPSSearchPeriod
milliseconds. That gives plenty of time for the small serial read buffer to overflow and lose the data.
Instead you should be reading all the time regardless of your desire to only do things periodically. Sure, report the data periodically, but you must read all the time as well.
-
Just to be sure you are talking about the Serial buffer ? "reading all the time" wouldn't that drain the battery ? my goal was like to make one GPS search each 2 seconde to save batteryRyan– Ryan06/06/2017 17:55:00Commented Jun 6, 2017 at 17:55
-
The SoftwareSerial buffer, yes. Unless you are sleeping the MCU reading all the time will make no difference. You are just looking at the content of the serial buffer.Majenko– Majenko06/06/2017 18:24:31Commented Jun 6, 2017 at 18:24
-
thank you very much, now i can read properly the data. last question i tried putting the while (ss.available() > 0) with declaration of the gps in a function that i call every time as u said to save sram (the SoftwareSerial is still global) i had no output is it because the decalring the gps as local variable ?Ryan– Ryan06/06/2017 18:32:04Commented Jun 6, 2017 at 18:32
-
Yes. You need the gps instance to either be global or local and
static
- the effect is the same (just the scope is different).Majenko– Majenko06/06/2017 21:03:01Commented Jun 6, 2017 at 21:03