SHARE
TWEET

Untitled

a guest Jan 20th, 2020 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. // Choose two Arduino pins to use for software serial
  5. int RXPin = 2;
  6. int TXPin = 3;
  7.  
  8. int GPSBaud = 9600;
  9.  
  10. // Create a TinyGPS++ object
  11. TinyGPSPlus gps;
  12.  
  13. // Create a software serial port called "gpsSerial"
  14. SoftwareSerial gpsSerial(RXPin, TXPin);
  15.  
  16. void setup()
  17. {
  18.   // Start the Arduino hardware serial port at 9600 baud
  19.   Serial.begin(9600);
  20.  
  21.   // Start the software serial port at the GPS's default baud
  22.   gpsSerial.begin(GPSBaud);
  23. }
  24.  
  25. void loop()
  26. {
  27.   // This sketch displays information every time a new sentence is correctly encoded.
  28.   while (gpsSerial.available() > 0)
  29.     if (gps.encode(gpsSerial.read()))
  30.       displayInfo();
  31.  
  32.   // If 5000 milliseconds pass and there are no characters coming in
  33.   // over the software serial port, show a "No GPS detected" error
  34.   if (millis() > 5000 && gps.charsProcessed() < 10)
  35.   {
  36.     Serial.println("No GPS detected");
  37.     while(true);
  38.   }
  39. }
  40.  
  41. void displayInfo()
  42. {
  43.   if (gps.location.isValid())
  44.   {
  45.     Serial.print("Latitude: ");
  46.     Serial.println(gps.location.lat(), 6);
  47.     Serial.print("Longitude: ");
  48.     Serial.println(gps.location.lng(), 6);
  49.     Serial.print("Altitude: ");
  50.     Serial.println(gps.altitude.meters());
  51.   }
  52.   else
  53.   {
  54.     Serial.println("Location: Not Available");
  55.   }
  56.  
  57.   Serial.print("Date: ");
  58.   if (gps.date.isValid())
  59.   {
  60.     Serial.print(gps.date.month());
  61.     Serial.print("/");
  62.     Serial.print(gps.date.day());
  63.     Serial.print("/");
  64.     Serial.println(gps.date.year());
  65.   }
  66.   else
  67.   {
  68.     Serial.println("Not Available");
  69.   }
  70.  
  71.   Serial.print("Time: ");
  72.   if (gps.time.isValid())
  73.   {
  74.     if (gps.time.hour() < 10) Serial.print(F("0"));
  75.     Serial.print(gps.time.hour());
  76.     Serial.print(":");
  77.     if (gps.time.minute() < 10) Serial.print(F("0"));
  78.     Serial.print(gps.time.minute());
  79.     Serial.print(":");
  80.     if (gps.time.second() < 10) Serial.print(F("0"));
  81.     Serial.print(gps.time.second());
  82.     Serial.print(".");
  83.     if (gps.time.centisecond() < 10) Serial.print(F("0"));
  84.     Serial.println(gps.time.centisecond());
  85.   }
  86.   else
  87.   {
  88.     Serial.println("Not Available");
  89.   }
  90.  
  91.   Serial.println();
  92.   Serial.println();
  93.   delay(1000);
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top