05-03-2026, 12:49
Witam. Robię sobie prędkościomierz do auta na esp32 w Arduino iDE. Potrzebuję pomocy by ktoś poprawił mój skrypt tak by prędkość była wyświetlana na środku ekranu oled 128x64 SSD1306. Poniżej skrypt który dostosowałem do swoich potrzeb ale wyśrodkowanie prędkości mnie przerasta. Proszę o weryfikację skryptu. Z góry dziękuję. Pozdrawiam.
#include <TinyGPSPlus.h>
#include <Wire.h> // I2C communication library
#include <Adafruit_GFX.h> // Graphics library for OLED
#include <Adafruit_SSD1306.h> // SSD1306 OLED display driver
// OLED display dimensions (128x64 pixels standard)
#define SCREEN_WIDTH 128 // OLED screen width in pixels
#define SCREEN_HEIGHT 64 // OLED screen height in pixels
#define ADDRESS 0x3C // Replace this with the I2C address of your OLED display
// OLED reset pin configuration (-1 means use Arduino reset pin)
#define OLED_RESET -1 // No dedicated reset pin, share Arduino reset
// Create OLED display object with specified dimensions, I2C interface, and reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Create GPS parser object
TinyGPSPlus gps; // TinyGPSPlus object for NMEA sentence parsing
// Define GPS serial port and pin assignments for ESP32 DEV KIT V1
#define gpsSerial Serial2 // Use hardware Serial2 (UART2) for GPS
#define RX_PIN 16 // ESP32 RX2/GPIO16 (RX pin) GPS TX
#define TX_PIN 17 // ESP32 TX2/GPIO17 (TX pin) GPS RX (optional)
// Setup function - runs once at startup
void setup() {
// Initialize GPS serial port on Serial2 with specified pins and GPS baud rate (9600)
gpsSerial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // 8 data bits, no parity, 1 stop bit
// Initialize OLED display with internal power supply and I2C address
if (!display.begin(SSD1306_SWITCHCAPVCC, ADDRESS)) { // Try to start OLED at I2C address
Serial.println(F("SSD1306 allocation failed")); // Print error if OLED fails
for (;;); // Infinite loop if OLED fails
}
// Clear OLED display buffer
display.clearDisplay();
// Configure OLED text rendering settings
display.setTextSize(1); // Set text size to 1 (smallest)
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0); // Set text cursor to top-left corner (x=0, y=0)
// Display startup message on OLED
display.println("Waiting for GPS...");
display.display(); // Update OLED with buffer contents
}
// Main loop - runs continuously
void loop() {
// Read all available GPS data from serial port
while (gpsSerial.available() > 0) {
// Read one byte from GPS serial port
if (gps.encode(gpsSerial.read())) { // Parse NMEA sentence, returns true if valid
displayLocationInfo(); // Update display with GPS data when valid fix
}
}
// Check if 5 seconds passed with no GPS data (less than 10 chars processed)
if (millis() > 5000 && gps.charsProcessed() < 10) {
display.clearDisplay(); // Clear OLED buffer
display.setCursor(0, 0); // Reset cursor to top-left
display.println("No GPS detected!"); // Show error message
display.display(); // Update OLED display
while (true); // Stop program execution
}
}
// Function to display GPS location information on OLED
void displayLocationInfo() {
display.clearDisplay(); // Clear previous display content
display.setCursor(35, 4);
display.setTextSize(1);
display.print("FIAT PUNTO");
display.setCursor(21, 16);
display.setTextSize(5);
display.print(gps.speed.kmph(), 0);
display.setCursor(3, 56);
display.setTextSize(1);
display.print("Speed km/h");
display.display();
delay(1500);
display.clearDisplay();
}
#include <TinyGPSPlus.h>
#include <Wire.h> // I2C communication library
#include <Adafruit_GFX.h> // Graphics library for OLED
#include <Adafruit_SSD1306.h> // SSD1306 OLED display driver
// OLED display dimensions (128x64 pixels standard)
#define SCREEN_WIDTH 128 // OLED screen width in pixels
#define SCREEN_HEIGHT 64 // OLED screen height in pixels
#define ADDRESS 0x3C // Replace this with the I2C address of your OLED display
// OLED reset pin configuration (-1 means use Arduino reset pin)
#define OLED_RESET -1 // No dedicated reset pin, share Arduino reset
// Create OLED display object with specified dimensions, I2C interface, and reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Create GPS parser object
TinyGPSPlus gps; // TinyGPSPlus object for NMEA sentence parsing
// Define GPS serial port and pin assignments for ESP32 DEV KIT V1
#define gpsSerial Serial2 // Use hardware Serial2 (UART2) for GPS
#define RX_PIN 16 // ESP32 RX2/GPIO16 (RX pin) GPS TX
#define TX_PIN 17 // ESP32 TX2/GPIO17 (TX pin) GPS RX (optional)
// Setup function - runs once at startup
void setup() {
// Initialize GPS serial port on Serial2 with specified pins and GPS baud rate (9600)
gpsSerial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // 8 data bits, no parity, 1 stop bit
// Initialize OLED display with internal power supply and I2C address
if (!display.begin(SSD1306_SWITCHCAPVCC, ADDRESS)) { // Try to start OLED at I2C address
Serial.println(F("SSD1306 allocation failed")); // Print error if OLED fails
for (;;); // Infinite loop if OLED fails
}
// Clear OLED display buffer
display.clearDisplay();
// Configure OLED text rendering settings
display.setTextSize(1); // Set text size to 1 (smallest)
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0); // Set text cursor to top-left corner (x=0, y=0)
// Display startup message on OLED
display.println("Waiting for GPS...");
display.display(); // Update OLED with buffer contents
}
// Main loop - runs continuously
void loop() {
// Read all available GPS data from serial port
while (gpsSerial.available() > 0) {
// Read one byte from GPS serial port
if (gps.encode(gpsSerial.read())) { // Parse NMEA sentence, returns true if valid
displayLocationInfo(); // Update display with GPS data when valid fix
}
}
// Check if 5 seconds passed with no GPS data (less than 10 chars processed)
if (millis() > 5000 && gps.charsProcessed() < 10) {
display.clearDisplay(); // Clear OLED buffer
display.setCursor(0, 0); // Reset cursor to top-left
display.println("No GPS detected!"); // Show error message
display.display(); // Update OLED display
while (true); // Stop program execution
}
}
// Function to display GPS location information on OLED
void displayLocationInfo() {
display.clearDisplay(); // Clear previous display content
display.setCursor(35, 4);
display.setTextSize(1);
display.print("FIAT PUNTO");
display.setCursor(21, 16);
display.setTextSize(5);
display.print(gps.speed.kmph(), 0);
display.setCursor(3, 56);
display.setTextSize(1);
display.print("Speed km/h");
display.display();
delay(1500);
display.clearDisplay();
}

