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
1 Answer 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;
}
-
Thank you this works great !Omar Ben Salah– Omar Ben Salah09/08/2020 12:45:34Commented Sep 8, 2020 at 12:45
if
block ... addserial.println ( millis() );
before "Satellite Count:" .... add one also at "Hello there"