Advertisement
pippero

Untitled

Jan 29th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 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[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. {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. int LatLonCount = sizeof LatLonList / sizeof LatLonList[0];
  29. SeqButton pulsanteScritturaCoordinate; //AGGIUNTO
  30. #define PULSANTE_8 8 //AGGIUNTO
  31.  
  32. static const uint32_t GPSBaud = 9600;
  33. // The TinyGPS++ object
  34.  
  35. // For stats that happen every 5 seconds
  36. unsigned long last = 0UL;
  37. int ledPin = 13;
  38.  
  39. void writeCoords() {
  40. int address = 0;
  41. for (int i = 0; i < LatLonCount; i++) {
  42. EEPROM.put(address, LatLonList[i].Lat);
  43. address += sizeof(LatLonList[i].Lat);
  44. EEPROM.put(address, LatLonList[i].Lon);
  45. address += sizeof(LatLonList[i].Lon);
  46. }
  47. }
  48. void readCoords() {
  49. int address = 0;
  50. for (int i = 0; i < LatLonCount; i++) {
  51. EEPROM.get(address, LatLonList[i].Lat);
  52. address += sizeof(LatLonList[i].Lat);
  53. EEPROM.get(address, LatLonList[i].Lon);
  54. address += sizeof(LatLonList[i].Lon);
  55. }
  56. }
  57. void setup() {
  58. readCoords();
  59. Serial.begin(9600);
  60. ss.begin(GPSBaud);
  61. pinMode(ledPin, OUTPUT);
  62. pinMode(7, INPUT_PULLUP);
  63. pinMode(8, INPUT_PULLUP);
  64. pinMode(9, INPUT_PULLUP);
  65.  
  66. pulsanteScritturaCoordinate.init(PULSANTE_8, NULL, &f_ButtonRelease, false, LOW, 100); //AGGIUNTO
  67. }
  68.  
  69. void loop() {
  70.  
  71. pulsanteScritturaCoordinate.handler(); //AGGIUNTO
  72.  
  73. //simulo il tasto ma va fatta nel setup
  74. //if (digitalRead(7) == LOW) {
  75. //Serial.print(F(" leggi leggi "));
  76. //readCoords();
  77. // }
  78. if (digitalRead(9) == LOW) {
  79. Serial.print(F(" cancella tutte le posizioni"));
  80. //per azzerare la posizione di salvataggio
  81. //rimetto la variabile --posizione-- a 0
  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.  
  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. for (int x = 0; x < LatLonCount; x++) {
  113. distanceToDest[x] =
  114. TinyGPSPlus::distanceBetween(
  115. currentLat,
  116. currentLon,
  117. LatLonList[x].Lat,
  118. LatLonList[x].Lon);
  119.  
  120. courseToDest[x] =
  121. TinyGPSPlus::courseTo(
  122. currentLat,
  123. currentLon,
  124. LatLonList[x].Lat,
  125. LatLonList[x].Lon);
  126.  
  127. Serial.print(F("Distance:"));
  128. Serial.print(x); Serial.print(" Km ");
  129. Serial.print(distanceToDest[x] / 1000, 3); // *Prints distance to destination
  130. Serial.print(" angolazione : ");
  131. Serial.print(courseToDest[x]);
  132. Serial.println(F(" gradi"));
  133. Serial.print(F("direzione cardinale ["));
  134. Serial.print(TinyGPSPlus::cardinal(courseToDest[x]));
  135. Serial.println(F("]"));
  136. if (distanceToDest[x] < piu_vicino) {//creo valore
  137. posto = x; // point
  138. piu_vicino = distanceToDest[x] / 1000; //+ vicino
  139. }
  140. if (distanceToDest[x] / 1000 < 6.0500) { //
  141. a = x; // imposto soglia limite distanza
  142. }
  143. }
  144. Serial.print(piu_vicino);
  145. Serial.print(F(" Km piu' vicino ")); Serial.println(posto);
  146. if (a < 7) {
  147. Serial.print("We have arrived.");
  148. Serial.println(a);
  149. digitalWrite(ledPin, HIGH);
  150. a = 8;
  151. }
  152. else
  153. {
  154. digitalWrite(ledPin, LOW);
  155. a = 8;
  156. }
  157. }
  158. if (gps.charsProcessed() < 10)
  159. Serial.println(F("WARNING: No GPS data. Check wiring."));
  160. }
  161.  
  162.  
  163. //AGGIUNTO
  164. //ogni volta che viene premuto e rilasciato il pulsante viene salvata una nuova posizione GPS
  165. void f_ButtonRelease()
  166. {
  167. static int Posizione=0;
  168.  
  169. if(gps.location.isValid())
  170. {
  171. LatLonList[Posizione].Lat = gps.location.lat();
  172. LatLonList[Posizione].Lon = gps.location.lng();
  173. Posizione = (Posizione + 1)%LatLonCount;
  174. Serial.print(F(" scrivi nuova posizione posizione: "));
  175. Serial.println(Posizione);
  176. Serial.print(F("LatLonCount: "));
  177. Serial.println(LatLonCount);
  178. writeCoords();
  179. }
  180. else
  181. {
  182. Serial.println(F("Modulo GPS non pronto - memorizzazione posizione fallita"));
  183. }
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement