Advertisement
pippero

gps_modificato

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