GPS! Print only new data
Backy
Posts: 1
Hello everyone! Sorry for bothering, I need help: I have GPS and code http://learn.parallax.com/KickStart/28500
But in loop every 5 second same data sent, its little bit messy ,I wanna print only new sensors data not the same with previous . If you have advice or some examples pls share with me. I use arduino Uno
For example result is :
Reading GPS
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
It will be better if it will be printed like this :
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745
But in loop every 5 second same data sent, its little bit messy ,I wanna print only new sensors data not the same with previous . If you have advice or some examples pls share with me. I use arduino Uno
[/FONT][/COLOR][COLOR=#333333][FONT=Lucida Console]#include <SoftwareSerial.h>[/FONT][/COLOR] #include "./TinyGPS.h" // Special version for 1.0TinyGPS gps;SoftwareSerial nss(6, 255); // Yellow wire to pin 6void setup() { Serial.begin(115200); nss.begin(4800); Serial.println("Reading GPS");}void loop() { bool newdata = false; unsigned long start = millis(); while (millis() - start < 5000) { // Update every 5 seconds if (feedgps()) newdata = true; } if (newdata) { gpsdump(gps); }}// Get and process GPS datavoid gpsdump(TinyGPS &gps) { float flat, flon; unsigned long age; gps.f_get_position(&flat, &flon, &age); Serial.print(flat, 4); Serial.print(", "); Serial.println(flon, 4);}// Feed data as it becomes available bool feedgps() { while (nss.available()) { if (gps.encode(nss.read())) return true; } return false; [COLOR=#333333][FONT=Lucida Console]}[/FONT][/COLOR][COLOR=#666666][FONT=TyponineSans Regular 18]
For example result is :
Reading GPS
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
It will be better if it will be printed like this :
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745
Comments
The easiest way to do this is to record the previous values:
Then, have an if statement:
Read the GPS data and store it in the "newdata" variable.
Compare the "newdata" to the "olddata".
If they are the same go read the GPS data.
If they are different move the "newdata" to the "olddata" and print the "olddata" and go read the GPS data.
This is basically what I do to minimize data being logged for trending, but also include the time the data was read.