2

I have an Adafruit Ultimate GPS connected to a MEGA2560. I am trying to get the data from the buffer and do other data processing at the same time, however. when i allow other processing to be done the output becomes garbled such as below. the first out is correct and then becomes garbled and outputs now useful data.

16:34:54.466 -> $GPGGA,163453.155,,,,,0,00,,,M,,M,,*7F

16:34:57.111 -> $GPRMC,163453.155,V,,,,$GP$GPGGA,1$GPGGA,$GPGGA


 void loop() 
 {
 GetTotalPressure();
 char c = GPS.read();
 Serial.print(c);
 if (GPS.newNMEAreceived()) {
 if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
 return; // we can fail to parse a sentence in which case we should just wait for another
 }
 if (timer2 > millis()) timer2 = millis();
 if (millis() - timer2 > 1000) {
 timer2 = millis(); // reset the timer
 GPSScreenUpdate();
 SDcardSave();
 } 

When GetTotalPressue is commented out The GPS serial output is correct when it is no the error above happens.

GetTotalPressure is below the sensor is connected to a I2C bus

void GetTotalPressure() {
 Totalpressure = ms5611.readPressure();

Many Thanks

asked Feb 19, 2020 at 16:45
2

1 Answer 1

1

You are not reading the GPS often enough. The method Adafruit_GPS::read() is documented in the comments embedded in the source code:

Read one character from the GPS device.

Call very frequently and multiple times per opportunity or the buffer may overflow if there are frequent NMEA sentences. An 82 character NMEA sentence 10 times per second will require 820 calls per second, and once a loop() may not be enough. Check for newNMEAreceived() after at least every 10 calls, or you may miss some short sentences.

returns: The character that we received, or 0 if nothing was available

Accessing the I2C bus makes loop() slower, slow enough that you get a buffer overflow, as explained above. I suggest that, instead of reading a single character per loop iteration, you read as many as are present in the buffer:

void loop()
{
 GetTotalPressure();
 // Empty the GPS port receive buffer.
 while (GPS.available()) {
 char c = GPS.read();
 Serial.print(c);
 }
 // Parse any newly received NMEA sentence.
 if (GPS.newNMEAreceived()) { // clears the newNMEAreceived flag
 if (!GPS.parse(GPS.lastNMEA()))
 return;
 }
 // Display and log location every second.
 if (millis() - timer2 > 1000) {
 timer2 = millis(); // reset the timer
 GPSScreenUpdate();
 SDcardSave();
 }
}

As an aside, I removed the line

if (timer2 > millis()) timer2 = millis();

which serves no useful purpose. If you are trying to detect millis() overflows, you are almost certainly doing something wrong.

answered Feb 20, 2020 at 15:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.