GPS Speedometer

Hello I'm using an Arduino UNO R4 with a Neo 7m Gps and an 8x32 Dot Matrix. I've spent some time on the code with little success so now im wondering what I need to do to fix my code.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

SoftwareSerial ss(4, 3);  // RX, TX pins for GPS (Software Serial)

TinyGPS gps;  // Create an instance of TinyGPS

#define DATA_PIN    11
#define CLOCK_PIN   13
#define LOAD_PIN    10
#define MAX_DEVICES 4  // 4 x 8x8 matrices = 32x8 display

// Initialize the MAX7219 with FC8_HW (for 8x8 matrix configuration)
MD_MAX72XX mx = MD_MAX72XX(MD_MAX72XX::FC16_HW, DATA_PIN, CLOCK_PIN, LOAD_PIN, MAX_DEVICES);

// Digit patterns (8x8 matrix for digits 0-9)
const uint8_t digitPatterns[10][8] = {
  {0x7E, 0x09, 0x09, 0x09, 0x7E, 0x00, 0x00, 0x00}, // 0
  {0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00}, // 1
  {0x7E, 0x01, 0x7E, 0x40, 0x7E, 0x00, 0x00, 0x00}, // 2
  {0x7E, 0x01, 0x7E, 0x01, 0x7E, 0x00, 0x00, 0x00}, // 3
  {0x7E, 0x40, 0x7E, 0x01, 0x01, 0x00, 0x00, 0x00}, // 4
  {0x7E, 0x40, 0x7E, 0x01, 0x7E, 0x00, 0x00, 0x00}, // 5
  {0x7E, 0x40, 0x7E, 0x49, 0x7E, 0x00, 0x00, 0x00}, // 6
  {0x7E, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00}, // 7
  {0x7E, 0x49, 0x7E, 0x49, 0x7E, 0x00, 0x00, 0x00}, // 8
  {0x7E, 0x49, 0x7E, 0x01, 0x7E, 0x00, 0x00, 0x00}  // 9
};

void setup() {
  // Start serial communication for debugging
  Serial.begin(115200);
  ss.begin(9600);  // GPS Serial

  // Initialize MAX7219
  mx.begin();
  mx.clear();  // Clear the display initially
  delay(500);  // Give time for hardware to initialize

  // Set the intensity of all devices to a reasonable level (e.g., intensity 5)
  for (int i = 0; i < MAX_DEVICES; i++) {
    mx.control(i, MD_MAX72XX::INTENSITY, 5);  // Set intensity to 5 (out of 15)
  }

  Serial.println("MAX7219 Initialized");

  // Initialize GPS
  // GPS setup can be handled in the loop by continuously reading GPS data
}

void loop() {
  // Read GPS data
  while (ss.available() > 0) {
    gps.encode(ss.read());
  }

  // Check if new GPS data is available and valid
  float latitude, longitude;
  float speed;

  // Retrieve latitude, longitude, and speed
  gps.f_get_position(&latitude, &longitude);
  speed = gps.speed();  // Speed in km/h

  // If valid GPS data is available
  if (latitude != TinyGPS::GPS_INVALID_ANGLE && longitude != TinyGPS::GPS_INVALID_ANGLE) {
    // Convert speed from km/h to mph
    float speedMPH = speed * 0.621371;
    displaySpeed(round(speedMPH));  // Round speed for display
  } else {
    displayMessage("Waiting for Fix");
  }

  delay(1000);  // Delay between updates
}

// Function to display speed on the MAX7219
void displaySpeed(float speed) {
  mx.clear();  // Clear the display first

  // Convert speed to an integer
  int speedInt = int(speed);
  String speedStr = String(speedInt);

  // If speed is more than 9999, just display the last four digits
  if (speedInt > 9999) {
    speedStr = String(speedInt).substring(speedStr.length() - 4);
  }

  // Display each digit (speedStr should contain the string representation of the speed)
  for (int i = 0; i < speedStr.length(); i++) {
    char digitChar = speedStr[i];  // Get the digit as a character
    int digit = digitChar - '0';   // Convert char to int (0–9)

    // Display the digit on the corresponding position on the matrix
    displayDigit(i * 8, digit);  // Display at the correct X position
  }

  // Update the display
  mx.update();
}

// Function to display an individual digit (8x8 matrix)
void displayDigit(int xOffset, int digit) {
  for (int row = 0; row < 8; row++) {
    uint8_t rowData = digitPatterns[digit][row]; // Get the row data for the digit
    for (int col = 0; col < 8; col++) {
      // Set each point on the display based on the rowData (1 = LED on, 0 = LED off)
      bool isOn = (rowData & (1 << (7 - col))) > 0; // Check if the LED should be on
      mx.setPoint(xOffset + col, row, isOn);       // Set the point on the matrix
    }
  }
}

// Function to display a custom message on the matrix
void displayMessage(String message) {
  int messageLength = message.length();
  int startX = 0;

  // Scroll the message across the matrix
  for (int i = 0; i < messageLength; i++) {
    char ch = message[i];
    
    // Find the corresponding digit pattern for the character
    int charIndex = ch - '0';  // This assumes the message only contains digits, modify if using letters.
    
    if (ch == ' ') {
      charIndex = -1; // Treat space as an empty pattern
    }

    // Display the character on the matrix at the current offset
    if (charIndex != -1) {
      for (int row = 0; row < 8; row++) {
        uint8_t rowData = digitPatterns[charIndex][row];
        for (int col = 0; col < 8; col++) {
          bool isOn = (rowData & (1 << (7 - col))) > 0;
          mx.setPoint(startX + col, row, isOn);
        }
      }
    }
    
    // Wait for a small amount of time to simulate scrolling
    delay(300);
    
    // Clear the matrix for the next character
    mx.clear();
    startX += 8;
  }
}

Believe it or not! You actually need to write down what you need to fix, We do not read minds, here.

Was this sketch perhaps generated by ChatGPT or one of its cousins?

It’s was not

I added the code

And be so kind to tell us what you think needs fixing.