Advertisement
pippero

GPS____mooood

Jan 29th, 2023 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #include <SeqButton.h> //AGGIUNTO
  2. #include <EEPROM.h>
  3. #include <TinyGPS++.h>
  4. #include <SoftwareSerial.h>
  5. TinyGPSPlus gps;
  6. static const int RXPin = 4, TXPin = 3;
  7. SoftwareSerial ss(RXPin, TXPin);
  8. float distanceToDest[50];
  9. float courseToDest[50];
  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. int Posizione = 0; //posizione memoria eeprom
  20.  
  21. struct LatLon LatLonList[50] =
  22. {
  23. //{0,0},
  24. //{0,0}, // United Arab Emirates
  25. //{0,0}, // mio44
  26. // {0,0}, // Albania
  27. // {0,0}, // Armenia
  28. // {0,0}, // Armenia
  29. //
  30. };
  31. int LatLonCount = Posizione;//------MODIFICATO----
  32. int SaveMax=sizeof LatLonList/sizeof LatLonList[0];//--MODIFICATO----
  33. //int LatLonCount = sizeof LatLonList / sizeof LatLonList[0];
  34. SeqButton pulsanteScritturaCoordinate; //AGGIUNTO
  35. #define PULSANTE_8 8 //AGGIUNTO
  36.  
  37. unsigned long last = 0UL;
  38. int ledPin = 13;
  39.  
  40.  
  41. void writeCoords() {
  42. int address = 0;
  43. for (int i = 0; i < LatLonCount; i++) {
  44. EEPROM.put(address, LatLonList[i].Lat);
  45. address += sizeof(LatLonList[i].Lat);
  46. EEPROM.put(address, LatLonList[i].Lon);
  47. address += sizeof(LatLonList[i].Lon);
  48. EEPROM.put(address, Posizione);
  49. address += sizeof(Posizione);
  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. EEPROM.get(address, Posizione);
  60. address += sizeof(Posizione);
  61. }
  62. }
  63. void setup() {
  64. readCoords();
  65. Serial.begin(9600);
  66. ss.begin(9600);
  67. pinMode(ledPin, OUTPUT);
  68. pinMode(7, INPUT_PULLUP);
  69. pinMode(8, INPUT_PULLUP);
  70. pinMode(9, INPUT_PULLUP);
  71.  
  72. pulsanteScritturaCoordinate.init(PULSANTE_8, NULL, &f_ButtonRelease, false, LOW, 100); //AGGIUNTO
  73. }
  74.  
  75. void loop() {
  76.  
  77. pulsanteScritturaCoordinate.handler(); //AGGIUNTO
  78.  
  79. if (digitalRead(9) == LOW) {
  80. Serial.print(F(" cancella tutte le posizioni"));
  81. //per azzerare la posizione di salvataggio
  82. //rimetto la variabile --posizione-- a 0
  83. }
  84. float currentLat, currentLon;
  85.  
  86. // Dispatch incoming characters
  87. while (ss.available() > 0) {
  88. gps.encode(ss.read());
  89. }
  90. if (gps.location.isUpdated() && millis() - last > 5000)
  91. {
  92. last = millis();
  93. currentLat = gps.location.lat();
  94. currentLon = gps.location.lng();
  95. Serial.print(F(" Lat="));
  96. Serial.print(currentLat, 6);
  97. Serial.print(F(" Long="));
  98. Serial.println(currentLon, 6);
  99. Serial.print(F(" course="));
  100. Serial.println(gps.course.deg());
  101.  
  102. if (gps.location.isValid())
  103. {
  104. Serial.println("Fix is valid.");
  105. }
  106. else
  107. {
  108. Serial.println("Fix is not yet valid.");
  109. return;
  110. }
  111.  
  112.  
  113. for (int x = 0; x < LatLonCount; x++) {
  114. distanceToDest[x] =
  115. TinyGPSPlus::distanceBetween(
  116. currentLat,
  117. currentLon,
  118. LatLonList[x].Lat,
  119. LatLonList[x].Lon);
  120.  
  121. courseToDest[x] =
  122. TinyGPSPlus::courseTo(
  123. currentLat,
  124. currentLon,
  125. LatLonList[x].Lat,
  126. LatLonList[x].Lon);
  127.  
  128. Serial.print(F("Distance:"));
  129. Serial.print(x); Serial.print(" Km ");
  130. Serial.print(distanceToDest[x] / 1000, 3); // *Prints distance to destination
  131. Serial.print(" angolazione : ");
  132. Serial.print(courseToDest[x]);
  133. Serial.println(F(" gradi"));
  134. Serial.print(F("direzione cardinale ["));
  135. Serial.print(TinyGPSPlus::cardinal(courseToDest[x]));
  136. Serial.println(F("]"));
  137. if (distanceToDest[x] < piu_vicino) {//creo valore
  138. posto = x; // point
  139. piu_vicino = distanceToDest[x] / 1000; //+ vicino
  140. }
  141. if (distanceToDest[x] / 1000 < 6.0500) { //
  142. a = x; // imposto soglia limite distanza
  143. }
  144. }
  145. Serial.print(piu_vicino);
  146. Serial.print(F(" Km piu' vicino ")); Serial.println(posto);
  147. if (a < 7) {
  148. Serial.print("We have arrived.");
  149. Serial.println(a);
  150. digitalWrite(ledPin, HIGH);
  151. a = 8;
  152. }
  153. else
  154. {
  155. digitalWrite(ledPin, LOW);
  156. a = 8;
  157. }
  158. }
  159. if (gps.charsProcessed() < 10)
  160. Serial.println(F("WARNING: No GPS data. Check wiring."));
  161. }
  162.  
  163.  
  164. //AGGIUNTO
  165. //ogni volta che viene premuto e rilasciato il pulsante viene salvata una nuova posizione GPS
  166. void f_ButtonRelease()
  167. {
  168. //static int Posizione=0;
  169.  
  170. if (gps.location.isValid())
  171. {
  172. LatLonList[Posizione].Lat = gps.location.lat();
  173. LatLonList[Posizione].Lon = gps.location.lng();
  174. Posizione = (Posizione + 1) % SaveMax;
  175. Serial.print(F(" scrivi nuova posizione posizione: "));
  176. Serial.println(Posizione);
  177. writeCoords();
  178. }
  179. else
  180. {
  181. Serial.println(F("Modulo GPS non pronto - memorizzazione posizione fallita"));
  182. }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement