Advertisement
pippero

GPS

Jan 27th, 2023 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include <TM1637.h>
  2. int CLK = 6;
  3. int DIO = 5;
  4. TM1637 tm(CLK,DIO);
  5.  
  6.  
  7. #include <EEPROM.h>
  8. float distanceToDest[10];
  9. float courseToDest[10];
  10. byte a = 22;
  11. int posto=0;
  12. float piu_vicino=999923.54;
  13. struct LatLon
  14. {
  15. float Lat;
  16. float Lon;
  17. };
  18.  
  19. struct LatLon LatLonList[] =
  20. {
  21. {38.210169, -75.689734},
  22. {38.210169, -75.689734}, // United Arab Emirates
  23. {45.383860, 9.2839777}, // mio44
  24. {41.153332, 20.168331}, // Albania
  25. {41.153332, 20.168331}, // Armenia
  26. {43.153332, 24.168331}, // Armenia
  27. };
  28. int LatLonCount = sizeof LatLonList / sizeof LatLonList[0];
  29. byte Index = 0;
  30.  
  31. #include <TinyGPS++.h>
  32. #include <SoftwareSerial.h>
  33. static const int RXPin = 4, TXPin = 3;
  34. static const uint32_t GPSBaud = 9600;
  35. // The TinyGPS++ object
  36. TinyGPSPlus gps;
  37. // The serial connection to the GPS device
  38. SoftwareSerial ss(RXPin, TXPin);
  39. // For stats that happen every 5 seconds
  40. unsigned long last = 0UL;
  41. int ledPin = 13;
  42.  
  43. void writeCoords(){
  44. int address = 0;
  45. for (int i = 0; i < LatLonCount; i++){
  46. EEPROM.put(address, LatLonList[i].Lat);
  47. address += sizeof(LatLonList[i].Lat);
  48. EEPROM.put(address, LatLonList[i].Lon);
  49. address += sizeof(LatLonList[i].Lon);
  50. }
  51. }
  52. void readCoords(){
  53. int address = 0;
  54. for (int i = 0; i < LatLonCount; i++){
  55. EEPROM.get(address, LatLonList[i].Lat);
  56. address += sizeof(LatLonList[i].Lat);
  57. EEPROM.get(address, LatLonList[i].Lon);
  58. address += sizeof(LatLonList[i].Lon);
  59. }
  60. }
  61. void displayNumber(int num){
  62. tm.display(3, num % 10);
  63. tm.display(2, num / 10 % 10);
  64. tm.display(1, num / 100 % 10);
  65. tm.display(0, num / 1000 % 10);
  66. }
  67.  
  68. void setup()
  69. {
  70. Serial.begin(9600);
  71. ss.begin(GPSBaud);
  72. pinMode(ledPin, OUTPUT);
  73. pinMode(7, INPUT_PULLUP);
  74. pinMode(8, INPUT_PULLUP);
  75. tm.init();
  76. tm.set(1);
  77. tm.display(0,1);
  78. tm.display(1,2);
  79. tm.set(2);
  80. tm.point(1);
  81. tm.display(2,3);
  82. tm.set(4);
  83. tm.display(3,3);
  84. }
  85.  
  86. void loop(){
  87. displayNumber(piu_vicino);
  88. if (digitalRead(7) == LOW) {
  89. Serial.print(F(" leggi leggi "));
  90. readCoords();
  91. }
  92. if (digitalRead(8) == LOW) {
  93. Serial.print(F(" scrivi scrivi "));
  94. readCoords();
  95. }
  96.  
  97. float currentLat, currentLon;
  98.  
  99. // Dispatch incoming characters
  100. while (ss.available() > 0){
  101. gps.encode(ss.read());
  102. tm.point(0);
  103. }
  104. while (ss.available()<= 0){
  105. tm.point(1);
  106. }
  107. if (gps.location.isUpdated() && millis() - last > 5000)
  108. {
  109. last = millis();
  110.  
  111. currentLat = gps.location.lat();
  112. currentLon = gps.location.lng();
  113. Serial.print(F(" Lat="));
  114. Serial.print(currentLat, 6);
  115. Serial.print(F(" Long="));
  116. Serial.println(currentLon, 6);
  117. Serial.print(F(" course="));
  118. Serial.println(gps.course.deg());
  119.  
  120. if (gps.location.isValid())
  121. {
  122. Serial.println("Fix is valid.");
  123. }
  124. else
  125. {
  126. Serial.println("Fix is not yet valid.");
  127. return;
  128. }
  129.  
  130. for (int x = 0; x < LatLonCount; x++) {
  131. distanceToDest[x] =
  132. TinyGPSPlus::distanceBetween(
  133. currentLat,
  134. currentLon,
  135. LatLonList[x].Lat,
  136. LatLonList[x].Lon);
  137.  
  138. courseToDest[x] =
  139. TinyGPSPlus::courseTo(
  140. currentLat,
  141. currentLon,
  142. LatLonList[x].Lat,
  143. LatLonList[x].Lon);
  144.  
  145. Serial.print(F("Distance:"));
  146. Serial.print(x); Serial.print(" Km ");
  147. Serial.print(distanceToDest[x] / 1000, 3); // *Prints distance to destination
  148. Serial.print(" angolazione : ");
  149. Serial.print(courseToDest[x]);
  150. Serial.println(F(" gradi"));
  151. Serial.print(F("direzione cardinale ["));
  152. Serial.print(TinyGPSPlus::cardinal(courseToDest[x]));
  153. Serial.println(F("]"));
  154. if (distanceToDest[x] < piu_vicino) {//creo valore
  155. posto=x; // point
  156. piu_vicino = distanceToDest[x]/1000; //+ vicino
  157. }
  158. if (distanceToDest[x] / 1000 < 6.0500) { //
  159. a = x; // imposto soglia limite distanza
  160. }
  161. }
  162. Serial.print(piu_vicino);
  163. Serial.print(F(" Km piu' vicino ")); Serial.println(posto);
  164. if (a < 7) {
  165. Serial.print("We have arrived.");
  166. Serial.println(a);
  167. digitalWrite(ledPin, HIGH);
  168. Index++;
  169. //delay(1500);
  170. if (Index >= LatLonCount)
  171. Index = 0; // Start over when we run out of destinations
  172. // EEPROM.put(0, Index);
  173. a = 8;
  174. }
  175. else
  176. {
  177. digitalWrite(ledPin, LOW);
  178. a = 8;
  179. delay(1500);
  180. }
  181.  
  182.  
  183. }
  184.  
  185. if (gps.charsProcessed() < 10)
  186. Serial.println(F("WARNING: No GPS data. Check wiring."));
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement