1

I am having some trouble using my gps (on Serial1) with my other sensors.

They both work great independently but when I use them in the same code, it's either the gps or the sensors that get to write on the Serial monitor.

For my gps I am using TinyGPS++, but I have a feeling that's not what's causing this problem here.

To simplify the code I'll be replacing what the sensors are supposed to write with this:

Serial.println("Hello there");

Here is the code

  if (Serial1.available() > 0) {
    gps.encode(Serial1.read());
    if (gps.location.isUpdated()){
      Serial.println("Satellite Count:");
      Serial.println(gps.satellites.value());
      Serial.println("Latitude:");
      Serial.println(gps.location.lat(), 6);
      Serial.println("Longitude:");
      Serial.println(gps.location.lng(), 6);
      Serial.println("Speed KT:");
      Serial.println(gps.speed.knots());
      Serial.println("Altitude Feet:");
      Serial.println(gps.altitude.feet());
      Serial.println("");
    }
  }
  Serial.println("Hello there");

I get this

Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there

If the line is removed, I get this

Satellite Count:
5
Latitude:
xx.xxxxx
Longitude:
-xx.xxxxxx
Speed KT:
0.19
Altitude Feet:
331.69

Thank you

7
  • add the sketch to the question? the GPS library returns data only if it is collected while your print in loop is executed in every loop
    – Juraj
    Sep 7, 2020 at 14:39
  • @Juraj excuse me, I don't understand. Can you please say again in other words ? Sep 7, 2020 at 14:51
  • print a timestamp inside the if block ... add serial.println ( millis() ); before "Satellite Count:" .... add one also at "Hello there"
    – jsotola
    Sep 7, 2020 at 16:51
  • @jsotola I changed it to text. My timestamp is actually just the number of seconds since 01 Jan 1970, like this Sep 7, 2020 at 17:49
  • 1
    both repeat to infinity Sep 7, 2020 at 20:22

1 Answer 1

1

This looks like another case of serial spam. I recommend changing to a frame-based approach and controlling that rate with checks to millis.

That limits the spam. Alternative is to limit it outright to 1:

//a static global:
static int isaidHELLO = 0;

//later in the loop
if (Serial1.available() > 0) {
    gps.encode(Serial1.read());
    if (gps.location.isUpdated()){
      Serial.println("Satellite Count:");
      Serial.println(gps.satellites.value());
      Serial.println("Latitude:");
      Serial.println(gps.location.lat(), 6);
      Serial.println("Longitude:");
      Serial.println(gps.location.lng(), 6);
      Serial.println("Speed KT:");
      Serial.println(gps.speed.knots());
      Serial.println("Altitude Feet:");
      Serial.println(gps.altitude.feet());
      Serial.println("");
      isaidHELLO = 0;
    }
  }
  if(isaidHELLO == 0)
  {
      Serial.println("Hello there");
      isaidHELLO = 1;
  }

1
  • Thank you this works great ! Sep 8, 2020 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.