TolentinoCotesta

Odometer

Jun 2nd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include "SSD1306Ascii.h"
  3. #include "SSD1306AsciiWire.h"
  4.  
  5. #include <NMEAGPS.h>
  6. #include <SoftwareSerial.h>
  7. //#include <NeoSWSerial.h>
  8.  
  9.  
  10. #define I2C_ADDRESS 0x3C
  11. #define RST_PIN -1
  12. SSD1306AsciiWire lcd;
  13.  
  14. NMEAGPS               gps;
  15. float                 odometer;
  16. NeoGPS::Location_t    lastLoc;
  17. bool                  lastLocOK = false;
  18. //NeoSWSerial           gpsPort(2, 3);
  19. static const int RXPin = 0, TXPin = 1;
  20. static const uint32_t GPSBaud = 9600;
  21. SoftwareSerial gpsPort(RXPin, TXPin);
  22.  
  23. const int SHOW_INTERVAL = 1; // 12;
  24. const int INITIAL_SHOW  = (2 * SHOW_INTERVAL) - 1; // works for any SHOW_INTERVAL
  25.       int show          = INITIAL_SHOW;
  26.  
  27. const int   BUZZER_PIN  = 4;
  28. const float SPEED_LIMIT = 0.1; //55.0; // kph
  29.  
  30.  
  31. void setup()
  32. {
  33.   Serial.begin(9600);
  34.   gpsPort.begin(GPSBaud);
  35.  
  36.   Wire.begin();
  37.   lcd.begin(&Adafruit128x64, I2C_ADDRESS);
  38.   lcd.setFont(System5x7);
  39.   lcd.clear();
  40.   lcd.println(" ");
  41.   lcd.println("GPS DATA LOGGER");
  42.  
  43.   delay(3000);
  44.   lcd.clear();
  45.  
  46. } // setup()
  47.  
  48.  
  49. void loop()
  50. {
  51.   // Parse any available GPS characters
  52.   if (gps.available( gpsPort )) {
  53.  
  54.     //  Enough GPS characters have been parsed to produce a fix structure.
  55.     //     This happens exactly once per second.
  56.     gps_fix fix = gps.read();  // get all the GPS fields
  57.     show = (show + 1) % SHOW_INTERVAL;
  58.  
  59.     // Check the speed
  60.     if (fix.valid.speed && (fix.speed_kph() > SPEED_LIMIT)) {
  61.       digitalWrite( BUZZER_PIN, HIGH );
  62.       Serial.println( F("Too fast!") );
  63.     } else {
  64.       digitalWrite( BUZZER_PIN, LOW );
  65.     }
  66.  
  67.     // Update the odometer
  68.     if (fix.valid.location) { // && fix.valid.speed && (fix.speed_kph() > MIN_SPEED)
  69.       if (lastLocOK) {
  70.         odometer += fix.location.DistanceKm( lastLoc );
  71.       }
  72.  
  73.       // ...and save this location for next time
  74.       lastLoc   = fix.location;
  75.       lastLocOK = true;
  76.     }
  77.  
  78.     // Update the LCD (oled)
  79.     if (show == 0) {
  80.  
  81.       #define MAX_CHARS 20
  82.       char displayBufffer[MAX_CHARS];
  83.      
  84.       // usando un buffer di dimensione fissa displayBufffer, la lunghezza del testo sarà sempre uguale
  85.       // es:  Odometer = 256.5 sarà stampato sul display il testo "Odometer: 256.50 Km"
  86.       // es:  Odometer = 26.0 sarà stampato sul display il testo  "Odometer:  26.00 Km"
  87.  
  88.       // Per ulteriori chiarimenti ti rimando alla documentazione C++ per l'istruzione snprintf
  89.       snprintf(displayBufffer, MAX_CHARS, "Odometer: % 3d.%02d Km", (int)odometer, (int)(odometer * 100)%100);
  90.       lcd.print(displayBufffer);
  91.     }
  92.  
  93.     // Print out a few things.  DON'T PRINT TOO MUCH!
  94.     Serial.print( F("LAT=") );
  95.     if (fix.valid.location)
  96.       Serial.print( fix.latitude(), 6);
  97.     Serial.print( F(" LON=") );
  98.     if (fix.valid.location)
  99.       Serial.print( fix.longitude(), 6);
  100.     Serial.print( F(" SAT=") );
  101.     if (fix.valid.satellites)
  102.       Serial.print( fix.satellites );
  103.     Serial.print( F(" Speed=") );
  104.     if (fix.valid.speed)
  105.       Serial.print( fix.speed_kph() );
  106.  
  107.     Serial.print( F(" ODO=") );
  108.     Serial.print( odometer );
  109.     Serial.print( F(" km") );
  110.     Serial.println();
  111.   }
  112.  
  113.   // Make sure *something* is being received!
  114.   if ((show == INITIAL_SHOW) && (millis() > 5000)) {
  115.     if (gps.statistics.chars == 0) {
  116.       Serial.println( F("** No characters received from GPS: check wiring **") );
  117.     } else {
  118.       Serial.println( F("** Characters are being received from GPS, but no valid data was parsed: check baud rate. **") );
  119.     }
  120.  
  121.     lcd.clear();
  122.     lcd.println(F("No data from GPS"));
  123.     lcd.println(F("(Timeout 5s)"));
  124.   }
  125.    
  126. }
Add Comment
Please, Sign In to add comment