Shop OBEX P1 Docs P2 Docs Learn Events
GPS! Print only new data — Parallax Forums

GPS! Print only new data

BackyBacky Posts: 1
edited 2014-08-05 08:35 in Learn with BlocklyProp
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
[/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

  • SRLMSRLM Posts: 5,045
    edited 2014-08-05 05:50
    Hi Backy. Unfortunately, your code is almost unreadable. Can you edit you post and make sure that it comes out nicely formatted.

    The easiest way to do this is to record the previous values:
    float flat_previous, flon_previous
    

    Then, have an if statement:
    if (flat_previous != flat || flon_previous != flon){
       // GPS has changed here.
    } else {
       // GPS has not changed
    }
    
  • kwinnkwinn Posts: 8,697
    edited 2014-08-05 08:35
    Basically do what SLRM suggested.

    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.
Sign In or Register to comment.