/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "Odometer Display"
- Source Code NOT compiled for: Arduino Nano
- Source Code created on: 2024-07-15 19:45:21
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* creare un contachilometri gps */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
#include // https://github.com/olikraus/U8g2_for_Adafruit_GFX
#include // GPS library
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void displayOdometer(float distance);
/***** DEFINITION OF I2C PINS *****/
const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // 60 in decimal
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance of U8G2_FOR_ADAFRUIT_GFX
TinyGPSPlus gps; // Create an instance of TinyGPSPlus
// Variables to store GPS data
float totalDistance = 0.0;
float lastLatitude = 0.0;
float lastLongitude = 0.0;
void setup(void)
{
Serial.begin(9600); // Initialize serial communication for GPS
display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
u8g2_for_adafruit_gfx.begin(display);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("GPS Odometer");
display.display();
delay(2000);
}
void loop(void)
{
while (Serial.available() > 0) {
gps.encode(Serial.read());
if (gps.location.isUpdated()) {
float currentLatitude = gps.location.lat();
float currentLongitude = gps.location.lng();
if (lastLatitude != 0.0 && lastLongitude != 0.0) {
float distance = TinyGPSPlus::distanceBetween(
lastLatitude, lastLongitude,
currentLatitude, currentLongitude
);
totalDistance += distance;
}
lastLatitude = currentLatitude;
lastLongitude = currentLongitude;
displayOdometer(totalDistance);
}
}
}
void displayOdometer(float distance) {
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(0,20);
u8g2_for_adafruit_gfx.print("Distance:");
u8g2_for_adafruit_gfx.setCursor(0,40);
u8g2_for_adafruit_gfx.print(distance, 2);
u8g2_for_adafruit_gfx.print(" meters");
display.display();
}