Pages: [1]   Go Down
Author Topic: print only new sensor data  (Read 113 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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


here code:
Code:
#include <SoftwareSerial.h>
#include "./TinyGPS.h"                 // Special version for 1.0

TinyGPS gps;
SoftwareSerial nss(6, 255);            // Yellow wire to pin 6

void 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 data
void 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;
}
« Last Edit: August 05, 2014, 02:15:39 am by Backy » Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4398
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you only want to put the data in the database when it has changed, then each time you read it save it in a variable.  Next time you read the data compare it with the previous saved data and only put it in your database if it has changed.

Note, however, that the data from the GPS may fluctuate slightly on each read even if the unit has not moved.  You might like to consider only putting it in the database if it has changed by a significant amount.  Something like this
Code:
if abs( (newData - prevData) ) > 10)
  {
    //code to put the data in the database
  }
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is already a newdata variable in your code. What is that doing?

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for reply
yes I also think that way...but the problem is I am not a good in C programming  smiley-roll
How can I save previous data?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is already a newdata variable in your code. What is that doing?

...R


I think its just new sensor data , it will be the same with previous , only sending time is different

35.3610, 139.2745
35.3610, 139.2745
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4398
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes I also think that way...but the problem is I am not a good in C programming  smiley-roll
How can I save previous data?
Code:

float prevFlat = flat;
float prevFlon =  flon;
unsigned long prevAge = age;
 
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: