Advertisement
pippero

gps2

Jan 27th, 2023 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 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 setup() {
  62. Serial.begin(9600);
  63. ss.begin(GPSBaud);
  64. pinMode(ledPin, OUTPUT);
  65. pinMode(7, INPUT_PULLUP);
  66. pinMode(8, INPUT_PULLUP);
  67. pinMode(9, INPUT_PULLUP);
  68. }
  69.  
  70. void loop() {
  71. //simulo il tasto ma va fatta nel setup
  72. if (digitalRead(7) == LOW) {
  73. Serial.print(F(" leggi leggi "));
  74. readCoords();
  75. }
  76. if (digitalRead(8) == LOW) {
  77. Serial.print(F(" scrivi nuova posizione posiznione n:"));
  78. writeCoords();
  79. }
  80. if (digitalRead(9) == LOW) {
  81. Serial.print(F(" cancella tutte le posizioni"));
  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. Index++;
  151. //delay(1500);
  152. if (Index >= LatLonCount)
  153. Index = 0; // Start over when we run out of destinations
  154. // EEPROM.put(0, Index);
  155. a = 8;
  156. }
  157. else
  158. {
  159. digitalWrite(ledPin, LOW);
  160. a = 8;
  161. //delay(1500);
  162. }
  163. }
  164. if (gps.charsProcessed() < 10)
  165. Serial.println(F("WARNING: No GPS data. Check wiring."));
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement