safwan092

Untitled

Mar 30th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample code demonstrates the normal use of a TinyGPSPlus (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 = 4800;
  10.  
  11. // The TinyGPSPlus 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("FullExample.ino"));
  23. Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
  24. Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  25. Serial.println(F("by Mikal Hart"));
  26. Serial.println();
  27. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  28. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  29. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  30. }
  31.  
  32. void loop()
  33. {
  34. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  35.  
  36. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  37. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  38. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  39. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  40. printInt(gps.location.age(), gps.location.isValid(), 5);
  41. printDateTime(gps.date, gps.time);
  42. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  43. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  44. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  45. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  46.  
  47. unsigned long distanceKmToLondon =
  48. (unsigned long)TinyGPSPlus::distanceBetween(
  49. gps.location.lat(),
  50. gps.location.lng(),
  51. LONDON_LAT,
  52. LONDON_LON) / 1000;
  53. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  54.  
  55. double courseToLondon =
  56. TinyGPSPlus::courseTo(
  57. gps.location.lat(),
  58. gps.location.lng(),
  59. LONDON_LAT,
  60. LONDON_LON);
  61.  
  62. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  63.  
  64. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  65.  
  66. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  67.  
  68. printInt(gps.charsProcessed(), true, 6);
  69. printInt(gps.sentencesWithFix(), true, 10);
  70. printInt(gps.failedChecksum(), true, 9);
  71. Serial.println();
  72.  
  73. smartDelay(1000);
  74.  
  75. if (millis() > 5000 && gps.charsProcessed() < 10)
  76. Serial.println(F("No GPS data received: check wiring"));
  77. }
  78.  
  79. // This custom version of delay() ensures that the gps object
  80. // is being "fed".
  81. static void smartDelay(unsigned long ms)
  82. {
  83. unsigned long start = millis();
  84. do
  85. {
  86. while (ss.available())
  87. gps.encode(ss.read());
  88. } while (millis() - start < ms);
  89. }
  90.  
  91. static void printFloat(float val, bool valid, int len, int prec)
  92. {
  93. if (!valid)
  94. {
  95. while (len-- > 1)
  96. Serial.print('*');
  97. Serial.print(' ');
  98. }
  99. else
  100. {
  101. Serial.print(val, prec);
  102. int vi = abs((int)val);
  103. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  104. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  105. for (int i=flen; i<len; ++i)
  106. Serial.print(' ');
  107. }
  108. smartDelay(0);
  109. }
  110.  
  111. static void printInt(unsigned long val, bool valid, int len)
  112. {
  113. char sz[32] = "*****************";
  114. if (valid)
  115. sprintf(sz, "%ld", val);
  116. sz[len] = 0;
  117. for (int i=strlen(sz); i<len; ++i)
  118. sz[i] = ' ';
  119. if (len > 0)
  120. sz[len-1] = ' ';
  121. Serial.print(sz);
  122. smartDelay(0);
  123. }
  124.  
  125. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  126. {
  127. if (!d.isValid())
  128. {
  129. Serial.print(F("********** "));
  130. }
  131. else
  132. {
  133. char sz[32];
  134. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  135. Serial.print(sz);
  136. }
  137.  
  138. if (!t.isValid())
  139. {
  140. Serial.print(F("******** "));
  141. }
  142. else
  143. {
  144. char sz[32];
  145. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  146. Serial.print(sz);
  147. }
  148.  
  149. printInt(d.age(), d.isValid(), 5);
  150. smartDelay(0);
  151. }
  152.  
  153. static void printStr(const char *str, int len)
  154. {
  155. int slen = strlen(str);
  156. for (int i=0; i<len; ++i)
  157. Serial.print(i<slen ? str[i] : ' ');
  158. smartDelay(0);
  159. }
Add Comment
Please, Sign In to add comment