Advertisement
pleasedontcode

"Odometer Display" rev_01

Jul 15th, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Odometer Display"
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-07-15 19:45:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* creare un contachilometri gps */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <Adafruit_SSD1306.h>  // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  26. #include <U8g2_for_Adafruit_GFX.h>  // https://github.com/olikraus/U8g2_for_Adafruit_GFX
  27. #include <TinyGPS++.h>  // GPS library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void displayOdometer(float distance);
  33.  
  34. /***** DEFINITION OF I2C PINS *****/
  35. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  36. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  37. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // 60 in decimal
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. #define OLED_RESET 4
  41. Adafruit_SSD1306 display(OLED_RESET);
  42. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance of U8G2_FOR_ADAFRUIT_GFX
  43. TinyGPSPlus gps;  // Create an instance of TinyGPSPlus
  44.  
  45. // Variables to store GPS data
  46. float totalDistance = 0.0;
  47. float lastLatitude = 0.0;
  48. float lastLongitude = 0.0;
  49.  
  50. void setup(void)
  51. {
  52.   Serial.begin(9600); // Initialize serial communication for GPS
  53.   display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  54.   u8g2_for_adafruit_gfx.begin(display);
  55.   display.clearDisplay();
  56.   display.setTextSize(1);
  57.   display.setTextColor(WHITE);
  58.   display.setCursor(0,0);
  59.   display.println("GPS Odometer");
  60.   display.display();
  61.   delay(2000);
  62. }
  63.  
  64. void loop(void)
  65. {
  66.   while (Serial.available() > 0) {
  67.     gps.encode(Serial.read());
  68.     if (gps.location.isUpdated()) {
  69.       float currentLatitude = gps.location.lat();
  70.       float currentLongitude = gps.location.lng();
  71.       if (lastLatitude != 0.0 && lastLongitude != 0.0) {
  72.         float distance = TinyGPSPlus::distanceBetween(
  73.           lastLatitude, lastLongitude,
  74.           currentLatitude, currentLongitude
  75.         );
  76.         totalDistance += distance;
  77.       }
  78.       lastLatitude = currentLatitude;
  79.       lastLongitude = currentLongitude;
  80.       displayOdometer(totalDistance);
  81.     }
  82.   }
  83. }
  84.  
  85. void displayOdometer(float distance) {
  86.   display.clearDisplay();
  87.   u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
  88.   u8g2_for_adafruit_gfx.setCursor(0,20);
  89.   u8g2_for_adafruit_gfx.print("Distance:");
  90.   u8g2_for_adafruit_gfx.setCursor(0,40);
  91.   u8g2_for_adafruit_gfx.print(distance, 2);
  92.   u8g2_for_adafruit_gfx.print(" meters");
  93.   display.display();
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement