Advertisement
Guest User

GPSDistance

a guest
Apr 30th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. include "TinyGPS++.h"
  2. include "SoftwareSerial.h"
  3.  
  4. static const int RXPin = 6, TXPin = 5;
  5. static const uint32_t GPSBaud = 9600;
  6.  
  7. TinyGPSPlus gps;
  8. SoftwareSerial ss(RXPin, TXPin);
  9.  
  10. unsigned long last = 0UL;
  11.  
  12. int counter = 0;
  13.  
  14. float distance = 0.0;
  15. float old_lat = 0.000000;
  16. float old_lng = 0.000000;
  17. float fastestSpeed = 0.0;
  18. float gpsSpeed;
  19.  
  20. boolean doOnce = false;
  21. boolean newLat = false;
  22. boolean newLng = false;
  23.  
  24. void setup()
  25. {
  26.   Serial.begin(115200);
  27.   while (!Serial) {
  28.     ;
  29.   }
  30.   ss.begin(GPSBaud);
  31.   Serial.println("Ready!");
  32. }
  33.  
  34. void loop()
  35. {
  36.   // Dispatch incoming characters
  37.   while (ss.available() > 0)
  38.     gps.encode(ss.read());
  39.  
  40.   if (gps.speed.isUpdated())
  41.   {
  42.     gpsSpeed = gps.speed.kmph();
  43.  
  44.     if (gps.speed.kmph() > fastestSpeed) {
  45.       fastestSpeed = gps.speed.kmph();
  46.       //      Serial.print("Fastest Speed: ");
  47.       Serial.print(fastestSpeed);
  48.       Serial.println("km/h");
  49.     }
  50.   }
  51.  
  52.   // VERSION 1
  53.   if (millis() - last > 5000)
  54.   {
  55.     //Serial.println();
  56.  
  57.     if (gps.location.isValid())
  58.     {
  59.  
  60.       if (counter < 100) {
  61.         counter++;
  62.         distance = 0;
  63.       }
  64.  
  65.       double distanceToLondon =
  66.         TinyGPSPlus::distanceBetween(
  67.           gps.location.lat(),
  68.           gps.location.lng(),
  69.           old_lat,
  70.           old_lng);
  71.  
  72.       if (gpsSpeed > 1.0) {
  73.         if (distanceToLondon < 100) {
  74.           distance = distance + distanceToLondon;
  75.         }
  76.         if (newLat == true || newLng == true) {
  77.           float correctDistance = distance * 2;
  78.           //Serial.print(F("TRAVELED     Distance="));
  79.           //Serial.print(distanceToLondon, 6);
  80.           Serial.print("Distance: ");
  81.           float distancekm = distance / 1000;
  82.           Serial.print(distancekm, 2);
  83.           Serial.print("km");
  84.           Serial.print(", ");
  85.           Serial.print(distance, 2);
  86.  
  87.           Serial.print("m, ");
  88.           /*Serial.print(correctDistance);
  89.  
  90.             Serial.print(", ");*/
  91.           Serial.print(gpsSpeed);
  92.           Serial.println("km/h");
  93.         }
  94.       }
  95.       float dasLat = gps.location.lat();
  96.       if (old_lat != dasLat) {
  97.         old_lat = gps.location.lat();
  98.         newLat = true;
  99.       } else {
  100.         newLat = false;
  101.       }
  102.       float dasLng = gps.location.lng();
  103.       if (old_lng != dasLng) {
  104.         old_lng = gps.location.lng();
  105.         newLng = true;
  106.       } else {
  107.         newLng = false;
  108.       }
  109.     }
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement