safwan092

Untitled

Oct 27th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
  5. It requires the use of SoftwareSerial, and assumes that you have a
  6. 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  7. */
  8. static const int RXPin = 4, TXPin = 3;
  9. static const uint32_t GPSBaud = 9600;
  10.  
  11. // The TinyGPS++ object
  12. TinyGPSPlus gps;
  13.  
  14. // The serial connection to the GPS device
  15. SoftwareSerial ss(RXPin, TXPin);
  16.  
  17. void setup()
  18. {
  19. Serial.begin(115200);
  20. ss.begin(GPSBaud);
  21.  
  22. Serial.println(F("DeviceExample.ino"));
  23. Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
  24. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  25. Serial.println(F("by Mikal Hart"));
  26. Serial.println();
  27. }
  28.  
  29. void loop()
  30. {
  31. // This sketch displays information every time a new sentence is correctly encoded.
  32. while (ss.available() > 0)
  33. if (gps.encode(ss.read()))
  34. displayInfo();
  35.  
  36. if (millis() > 5000 && gps.charsProcessed() < 10)
  37. {
  38. Serial.println(F("No GPS detected: check wiring."));
  39. while(true);
  40. }
  41. }
  42.  
  43. void displayInfo()
  44. {
  45. Serial.print(F("Location: "));
  46. if (gps.location.isValid())
  47. {
  48. Serial.print(gps.location.lat(), 6);
  49. Serial.print(F(","));
  50. Serial.print(gps.location.lng(), 6);
  51. }
  52. else
  53. {
  54. Serial.print(F("INVALID"));
  55. }
  56.  
  57. Serial.print(F(" Date/Time: "));
  58. if (gps.date.isValid())
  59. {
  60. Serial.print(gps.date.month());
  61. Serial.print(F("/"));
  62. Serial.print(gps.date.day());
  63. Serial.print(F("/"));
  64. Serial.print(gps.date.year());
  65. }
  66. else
  67. {
  68. Serial.print(F("INVALID"));
  69. }
  70.  
  71. Serial.print(F(" "));
  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(F(":"));
  77. if (gps.time.minute() < 10) Serial.print(F("0"));
  78. Serial.print(gps.time.minute());
  79. Serial.print(F(":"));
  80. if (gps.time.second() < 10) Serial.print(F("0"));
  81. Serial.print(gps.time.second());
  82. Serial.print(F("."));
  83. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  84. Serial.print(gps.time.centisecond());
  85. }
  86. else
  87. {
  88. Serial.print(F("INVALID"));
  89. }
  90.  
  91. Serial.println();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment