Advertisement
pippero

Untitled

Jan 29th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 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. struct LatLon LatLonList[50] =
  20. {
  21. //{0,0},
  22. //{0,0}, // United Arab Emirates
  23. //{0,0}, // mio44
  24. // {0,0}, // Albania
  25. // {0,0}, // Armenia
  26. // {0,0}, // Armenia
  27. //
  28. };
  29. int LatLonCount = sizeof LatLonList / sizeof LatLonList[0];
  30. SeqButton pulsanteScritturaCoordinate; //AGGIUNTO
  31. #define PULSANTE_8 8 //AGGIUNTO
  32.  
  33. unsigned long last = 0UL;
  34. int ledPin = 13;
  35. int Posizione = 0; //posizione memoria eeprom
  36.  
  37. void writeCoords() {
  38. int address = 0;
  39. //for (int i = 0; i < LatLonCount; i++) {
  40. EEPROM.put(address, Posizione);
  41. address += sizeof(Posizione);
  42. EEPROM.put(address, Posizione);
  43. address += sizeof(Posizione);
  44. EEPROM.put(address, Posizione);
  45. address += sizeof(Posizione);
  46. //}
  47. }
  48. void readCoords() {
  49. int address = 0;
  50. //for (int i = 0; i < LatLonCount; i++) {
  51. EEPROM.get(address, Posizione);
  52. address += sizeof(Posizione);
  53. EEPROM.get(address, Posizione);
  54. address += sizeof(Posizione);
  55. EEPROM.get(address, Posizione);
  56. address += sizeof(Posizione);
  57. //}
  58. }
  59. void setup() {
  60. readCoords();
  61. Serial.begin(9600);
  62. ss.begin(9600);
  63. pinMode(ledPin, OUTPUT);
  64. pinMode(7, INPUT_PULLUP);
  65. pinMode(8, INPUT_PULLUP);
  66. pinMode(9, INPUT_PULLUP);
  67.  
  68. pulsanteScritturaCoordinate.init(PULSANTE_8, NULL, &f_ButtonRelease, false, LOW, 100); //AGGIUNTO
  69. }
  70.  
  71. void loop() {
  72.  
  73. pulsanteScritturaCoordinate.handler(); //AGGIUNTO
  74.  
  75. if (digitalRead(9) == LOW) {
  76. Serial.print(F(" cancella tutte le posizioni"));
  77. //per azzerare la posizione di salvataggio
  78. //rimetto la variabile --posizione-- a 0
  79. Posizione = 0;
  80. writeCoords();
  81.  
  82. }
  83. float currentLat, currentLon;
  84.  
  85. // Dispatch incoming characters
  86. while (ss.available() > 0) {
  87. gps.encode(ss.read());
  88. }
  89. if (gps.location.isUpdated() && millis() - last > 5000)
  90. {
  91. last = millis();
  92. currentLat = gps.location.lat();
  93. currentLon = gps.location.lng();
  94. Serial.print(F(" Lat="));
  95. Serial.print(currentLat, 6);
  96. Serial.print(F(" Long="));
  97. Serial.println(currentLon, 6);
  98. Serial.print(F(" course="));
  99. Serial.println(gps.course.deg());
  100.  
  101. if (gps.location.isValid())
  102. {
  103. Serial.println("Fix is valid.");
  104. }
  105. else
  106. {
  107. Serial.println("Fix is not yet valid.");
  108. return;
  109. }
  110.  
  111.  
  112. for (int x = 0; x < Posizione; x++) {//-----MODIFICATO------
  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) % LatLonCount;
  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.  
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement