TinyGPS and the while loop delay

TinyGPS has a millis function in a "while" loop, to assure new data is available before trying to write lat, lon etc.

Is the entire sketch slowed to the time period in this millis function?

I am using TinyGPS as well as code for reading an HMC6352 magnetic compass in a first attempt at making a "Marine Autopilot." Everything is printing out on my serial LCD, no problem.

The sketch is written with tabs to make it more readable. The compass reads update very fast if the call to the GPS read tab is commented out. When I un-comment the call to the GPS, everything slows down to that while loop delay.

I would like the response to the mag compass to be quicker than 1 second, as it is used in a comparison tab to turn a steering motor on/off, cw or ccw depending on deviation from a set heading.

The delay in the GPS info is not a problem, just the fact that is slows down the compass info and response.

John

Here is the code. The very start is the "while loop". This is the tab page in my sketch.

void GPS_read()

{
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
if (newdata)
{
gpsdump(gps);
}

}

void printFloat(double number, int digits)
{
// Handle negative numbers
if (number < 0.0)
{
Serial.print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
Serial.print(int_part);

// Print the decimal point, but only if there are digits beyond
if (digits > 0)
Serial.print(".");

// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
Serial.print(toPrint);
remainder -= toPrint;
}
}

// *******************************************************************

void gpsdump(TinyGPS &gps)
{
long lat, lon;

// If we don't feed the gps during this long routine,
// we may drop characters and get checksum errors
feedgps();
//gps.f_get_position(&flat, &flon);

GPS_print(); // GPS print routine

}

bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}

John,

I think a better structure for your program would be

void loop()
{
  check_and_display_the_compass();
  while (nss.available())
  {
    if (gps.encode(nss.read())
    {
      grab_and_display_gps_data();
      break;
    }
  }
}

Mikal

Thanks for the help, Mikalhart. I will re-write my loop, and let you know how it works.

John

Greetings Mikal:

I took the two sections/tabs of code, compass read and GPS read , cleaned them up, and wrote a new main body without any extraneous stuff the program had. I rewrote the void loop () in a much simplified version using your example, and it works great!!

Thank you for assisting me in this project.

Although I have been studying the TinyGPS code, I do not think I would have come up with the answer to my problem for a long time.

Hopefully, it will all gradually all sink in.

Yours respectfully,
John

John, thank you very much for the follow up.

I have decided that I really need to concoct a more fluid and easy-to-understand example for TinyGPS.

The basic idea of TinyGPS is that you feed it every character that arrives from the GPS (using encode) while it extracts pertinent data from the NMEA sentences that go flying by. TinyGPS's view of this data is updated at the end of each sentence.

When encode() returns "true", that tells you that TinyGPS has just completed analyzing a fresh sentence, so now might be a good time to inspect the position data to see if it's new.

Thanks again for sharing.

Mikal

One other thing to point out is that you don't need that printFloat() routine anymore. It was put into Arduino proper back a year ago.

float f = 3.14159;
Serial.print(f, 5);

Mikal