Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "SSD1306Ascii.h"
- #include "SSD1306AsciiWire.h"
- #include <NMEAGPS.h>
- #include <SoftwareSerial.h>
- //#include <NeoSWSerial.h>
- #define I2C_ADDRESS 0x3C
- #define RST_PIN -1
- SSD1306AsciiWire lcd;
- NMEAGPS gps;
- float odometer;
- NeoGPS::Location_t lastLoc;
- bool lastLocOK = false;
- //NeoSWSerial gpsPort(2, 3);
- static const int RXPin = 0, TXPin = 1;
- static const uint32_t GPSBaud = 9600;
- SoftwareSerial gpsPort(RXPin, TXPin);
- const int SHOW_INTERVAL = 1; // 12;
- const int INITIAL_SHOW = (2 * SHOW_INTERVAL) - 1; // works for any SHOW_INTERVAL
- int show = INITIAL_SHOW;
- const int BUZZER_PIN = 4;
- const float SPEED_LIMIT = 0.1; //55.0; // kph
- void setup()
- {
- Serial.begin(9600);
- gpsPort.begin(GPSBaud);
- Wire.begin();
- lcd.begin(&Adafruit128x64, I2C_ADDRESS);
- lcd.setFont(System5x7);
- lcd.clear();
- lcd.println(" ");
- lcd.println("GPS DATA LOGGER");
- delay(3000);
- lcd.clear();
- } // setup()
- void loop()
- {
- // Parse any available GPS characters
- if (gps.available( gpsPort )) {
- // Enough GPS characters have been parsed to produce a fix structure.
- // This happens exactly once per second.
- gps_fix fix = gps.read(); // get all the GPS fields
- show = (show + 1) % SHOW_INTERVAL;
- // Check the speed
- if (fix.valid.speed && (fix.speed_kph() > SPEED_LIMIT)) {
- digitalWrite( BUZZER_PIN, HIGH );
- Serial.println( F("Too fast!") );
- } else {
- digitalWrite( BUZZER_PIN, LOW );
- }
- // Update the odometer
- if (fix.valid.location) { // && fix.valid.speed && (fix.speed_kph() > MIN_SPEED)
- if (lastLocOK) {
- odometer += fix.location.DistanceKm( lastLoc );
- }
- // ...and save this location for next time
- lastLoc = fix.location;
- lastLocOK = true;
- }
- // Update the LCD (oled)
- if (show == 0) {
- #define MAX_CHARS 20
- char displayBufffer[MAX_CHARS];
- // usando un buffer di dimensione fissa displayBufffer, la lunghezza del testo sarà sempre uguale
- // es: Odometer = 256.5 sarà stampato sul display il testo "Odometer: 256.50 Km"
- // es: Odometer = 26.0 sarà stampato sul display il testo "Odometer: 26.00 Km"
- // Per ulteriori chiarimenti ti rimando alla documentazione C++ per l'istruzione snprintf
- snprintf(displayBufffer, MAX_CHARS, "Odometer: % 3d.%02d Km", (int)odometer, (int)(odometer * 100)%100);
- lcd.print(displayBufffer);
- }
- // Print out a few things. DON'T PRINT TOO MUCH!
- Serial.print( F("LAT=") );
- if (fix.valid.location)
- Serial.print( fix.latitude(), 6);
- Serial.print( F(" LON=") );
- if (fix.valid.location)
- Serial.print( fix.longitude(), 6);
- Serial.print( F(" SAT=") );
- if (fix.valid.satellites)
- Serial.print( fix.satellites );
- Serial.print( F(" Speed=") );
- if (fix.valid.speed)
- Serial.print( fix.speed_kph() );
- Serial.print( F(" ODO=") );
- Serial.print( odometer );
- Serial.print( F(" km") );
- Serial.println();
- }
- // Make sure *something* is being received!
- if ((show == INITIAL_SHOW) && (millis() > 5000)) {
- if (gps.statistics.chars == 0) {
- Serial.println( F("** No characters received from GPS: check wiring **") );
- } else {
- Serial.println( F("** Characters are being received from GPS, but no valid data was parsed: check baud rate. **") );
- }
- lcd.clear();
- lcd.println(F("No data from GPS"));
- lcd.println(F("(Timeout 5s)"));
- }
- }
Add Comment
Please, Sign In to add comment