[Solved] Software takes too long for Serial.read()

Hey fellow forum members,

I am reading from serial1 from my GPS Device and want to save the values in corresponding variables. This works standalone perfectly.

void loop() {
  while (Serial1.available() > 0) {
    if (gps.encode(Serial1.read())) {
      Serial.print("Valid= ");
      Serial.print(" Latitude= ");
      Serial.print(gps.location.lat(), 6);
      Serial.print(" Longitude= ");
      Serial.println(gps.location.lng(), 6);

Absolutly no problems here. I get several GPS Returns per second and this is awesome as standalone. When I implement the same code to my main Code, I see on my serial monitor, that it is oddly slow. This is no wonder though, since there are other works on the pipeline, which takes around 530ms and is dependend to GPS values. It is obvious, that I can't read from GPS Device, while the processor is busy doing other stuff.

Well I am not that experienced with Serial.reading but it feels on debugging like, there is a certain cache and it is ridden from front to end. Since my software is slower than the GPS input, I miss values and GPS device "lags" (4 minute delay, 4 minutes long same data returned over and over again)

GitHub Link to the Project Folder
Relevant Files: main.ino

What I need is just get me the last value. Is there any possibility for this in Serial (I almost think not because of Stream characteristic of Serials)

Thanks in advance

Is there any possibility for this in Serial

Imagine you have a bucket. Someone pouring water into the bucket faster than you can drain it out. What do you suppose will happen to the water that doesn't fit in the bucket?

The Serial object has such a bucket. The GPS is pouring data into the bucket faster than you can read it.

You MUST rewrite your code to ensure that you read the data faster than it arrives.

Yeah feared that... Thank you Paul...

Gotta create a cache for the function which takes 500ms. I increased the interval which fixed the problem but when I am near a Point of Interest, I am going to run this method more often, which will cause the problem...

Thanks a lot, I guess... I wished, there was an easier solution

The obvious answer is that 530ms is waaaaaay too long for your loop() function to execute. It's a sure sign that your code is not well-designed or thought out. You need to re-architect it so that what ever you're doing that takes that long is broken up into much smaller chunks and only a little bit is done every time loop() is invoked. That also gives you time to also do a little bit of Serial work in loop(). But, obviously, don't block on Serial either.

Hey gfvalvo,

I read 6000 flash memory entries and compare them to me. Earlier version of this created a cache of 32 entries and went through these 6000 only once a minute.

Well, since I optimized the calculation, whole process takes "only" 500ish ms instead of initial 3-4 seconds. I am actually happy with a refresh rate of half a second so I didn't minded.

Anyways, I have an easy solution now: I increased the interval of this 500ish ms process to once a second and Serial is read just perfectly.

Thanks for your help