I am trying to make a GPS KM counter.
Any thoughts on this code?
Any thoughts on this code?
Code:
#include <TinyGPS++.h>
#include <EEPROM.h>
TinyGPSPlus gps;
double PreviousLat; // Previos Latitude
double PreviousLng; // Previous Longitude
double DistanceBetween; // Distance between current and last saved position
double Km = 0; // Km
double KmCounter = 0; // Total vehicule km's
int KmStoreLocation; // EEPRROM Byte where km's are stored
bool PreviousPosition = false; // No previous position has been stored, change to true after storing a position
void GPSKMCounter(){
if (gps.location.isValid()) {
if (PreviousPosition == false) { // Store start position
PreviousLat = gps.location.lat();
PreviousLng = gps.location.lng();
PreviousPosition = true;
KmCounter = EEPROM.read(50); // Read current km's stored
if (KmCounter >= 100000) { // If reached 100.000KM move to next byte, EEPROM 100.000 write cycles
KmCounter = EEPROM.read(51);
}
if (KmCounter >= 200000) { // // If reached 200.000KM move to next byte, EEPROM 100.000 write cycles
KmCounter = EEPROM.read(52);
}
} else { // PreviousPosition == true
if(gps.distanceBetween(gps.location.lat(), gps.location.lng(), PreviousLat, PreviousLng) > 100) { // If traveled more then 100 meter
Km += (gps.distanceBetween(gps.location.lat(), gps.location.lng(), PreviousLat, PreviousLng)) / 1000; // KM travelled
if (Km >= 1) {
KmCounter += Km;
if (KmCounter < 100000) {
EEPROM.write(50, KmCounter);
} else if (KmCounter >= 100000 && KmCounter < 200000) {
EEPROM.write(51, KmCounter);
} else if (KmCounter >= 200000) {
EEPROM.write(52, KmCounter);
}
Km = 0; // Reset Km to 0
}
}
}
}
}