Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3.  
  4.  
  5. static const int RXPin = 21, TXPin = 23;
  6. static const uint32_t GPSBaud = 9600;
  7.  
  8. // The TinyGPS++ object
  9. TinyGPSPlus gps;
  10.  
  11. // The serial connection to the GPS device
  12. SoftwareSerial ss(RXPin, TXPin);
  13. void displayInfo();
  14.  
  15. void setup()
  16. {
  17. Serial.begin(9600);
  18. ss.begin(GPSBaud);
  19. }
  20.  
  21. void loop()
  22. {
  23. // This sketch displays information every time a new sentence is correctly encoded.
  24.  
  25. while (ss.available() > 0){
  26. if (gps.encode(ss.read()))
  27. displayInfo();
  28. }
  29.  
  30.  
  31. if (millis() > 5000 && gps.charsProcessed() < 10)
  32. {
  33. Serial.println(F("No GPS detected: check wiring."));
  34. while(true);
  35. }
  36. }
  37.  
  38. void displayInfo()
  39. {
  40. Serial.print(F("Location: "));
  41. if (gps.location.isValid())
  42. {
  43. Serial.print(gps.location.lat(), 6);
  44. Serial.print(F(","));
  45. Serial.print(gps.location.lng(), 6);
  46. }
  47. else
  48. {
  49. Serial.print(F("INVALID"));
  50. }
  51.  
  52. Serial.print(F(" Date/Time: "));
  53. if (gps.date.isValid())
  54. {
  55. Serial.print(gps.date.month());
  56. Serial.print(F("/"));
  57. Serial.print(gps.date.day());
  58. Serial.print(F("/"));
  59. Serial.print(gps.date.year());
  60. }
  61. else
  62. {
  63. Serial.print(F("INVALID"));
  64. }
  65.  
  66. Serial.print(F(" "));
  67. if (gps.time.isValid())
  68. {
  69. if (gps.time.hour() < 10) Serial.print(F("0"));
  70. Serial.print(gps.time.hour());
  71. Serial.print(F(":"));
  72. if (gps.time.minute() < 10) Serial.print(F("0"));
  73. Serial.print(gps.time.minute());
  74. Serial.print(F(":"));
  75. if (gps.time.second() < 10) Serial.print(F("0"));
  76. Serial.print(gps.time.second());
  77. Serial.print(F("."));
  78. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  79. Serial.print(gps.time.centisecond());
  80. }
  81. else
  82. {
  83. Serial.print(F("INVALID"));
  84. }
  85.  
  86. Serial.println();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement