Uno R3, Adafruit IS31FL3731 PWM led driver, and 16x9 Charlieplexed led issue

Hi. I could use a little assistance please. My diagram attached is for testing to get it right and then I will implement it into the car I am building it for. the pushbuttons are actually toggle switches for testing. It is just used to display the gear (R,N,1,2,3) the transmission is in. “R” displays fine, “N” displays fine, “1” will display fine when first triggered, but when I go to display “2” or then “3” or go back down to the prior “1”, or “2” as it will function in the car, the led display more times than not it will still have a previous digit displayed and it will be displaying both over top of each other. The car transmission has a lever to select between neutral, forward/first gear and reverse. When in forward/first gear the following gears,2 and 3 are then activated in order so when in 3rd gear all 3 inputs on my uno will be triggered. The LED Driver is connected via I2C. here is my code.

#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_IS31FL3731.h>
Adafruit_IS31FL3731 ledmatrix = Adafruit_IS31FL3731();

const int neutralinput = 2; // assigning pin 2 to the neutral switch
const int reversegearinput = 3; // assigning pin 3 to the reverse switch
const int firstgearinput = 4; // assigning pin 4 to the foward/first gear switch
const int secondgearinput = 5; // assigning pin 5 to the second gear switch
const int thirdgearinput = 6; // assigning pin 6 to the third gear switch

int gear(); //variable to compare to in my switch case






void setup() {
  Serial.begin(9600);
  if (! ledmatrix.begin()) {
    Serial.println("IS31 not found");
    while (1);
  }
  Serial.println("IS31 found!");

  pinMode(neutralinput, INPUT_PULLUP);  // designating the neutral safety switch input pin 2 as an input on the arduino and utilizing the internal resistors to bring high
  pinMode(reversegearinput, INPUT_PULLUP); // designating the reverse switch input pin 3 as an input on the arduino and utilizing the internal resistors to bring high
  pinMode(firstgearinput, INPUT_PULLUP); // designating the foward/first gear switch input pin 4 as an input on the arduino and utilizing the internal resistors to bring high
  pinMode(secondgearinput, INPUT_PULLUP); // designating the 2nd gear switch input pin 5 as an input on the arduino and utilizing the internal resistors to bring high
  pinMode(thirdgearinput, INPUT_PULLUP); // designating the 3rd gear switch input pin 6 as an input on the arduino and utilizing the internal resistors to bring high


  ledmatrix.setTextColor(50); //setting the led display paramters so the characters display properly
  ledmatrix.setCursor(-1, 1);
  ledmatrix.setTextSize(2);
  ledmatrix.setRotation(1);

  // put your setup code here, to run once:

}
void loop() {
   int gear =(0);
  // the digitalReads are to set my variable for the actions happening
  if (digitalRead(neutralinput) ==LOW)
      gear =(1); 
  if (digitalRead(reversegearinput) ==LOW)
      gear =(2);
  if (digitalRead(firstgearinput) == LOW && digitalRead(secondgearinput) == HIGH && digitalRead(thirdgearinput) == HIGH)
      gear =(3);
  if (digitalRead(firstgearinput) == LOW && digitalRead(secondgearinput) == LOW && digitalRead(thirdgearinput) == HIGH)
      gear =(4);
  if (digitalRead(firstgearinput) == LOW && digitalRead(secondgearinput) == LOW && digitalRead(thirdgearinput) == LOW)
      gear =(5);
    
 switch(gear) {
  case 1:
    ledmatrix.print("N") ;
      break;
  case 2:
    ledmatrix.print("R") ;
      break;
   case 3:
    ledmatrix.print("1");
      break; 
   case 4:
    ledmatrix.print("2");
      break; 
   case 5:
    ledmatrix.print("3"); 
      break;
   default:
    ledmatrix.clear();
      break;
      }  
  }

I have changed my ledmatrix.print(“1,2,or 3”) to Serial.print(“1,2, or 3”) instead and watch the serial monitor as I was switching the different toggles for 1st, 2nd and 3rd gear on and off and it was printing properly as the switching happened. I’ve tried adding ledmatrix.clear(), or a delay in between each case before or after the break and at the top of the switch and still will have the overlapping numbers displayed most of the time. ive spent days reading different forums, instructional posts and watched a ton of videos to try and learn this, I’m sure basic program to most, as it is my first time doing any type of programming at all. I am amazed at what can be done and plan to keep learning by trying but I am at a roadblock with this. Any suggestions are greatly appreciated.

Thank you,
Mike.

I've tried adding ledmatrix.clear(),

That sounds what you need to do but you need to do it only when a new gear is selected. Create a new variable, perhaps named previousGear, and copy gear to it before your series of checks on the current gear. If the gear has changed then clear the matrix and display the current gear.

UKHeliBob:
That sounds what you need to do but you need to do it only when a new gear is selected. Create a new variable, perhaps named previousGear, and copy gear to it before your series of checks on the current gear. If the gear has changed then clear the matrix and display the current gear.

Thankyou for the response UKHeliBob,
I apologize but I don't quite understand what you mean. I've tried adding int previousGear() = gear and I get error message of gear not being a comparison operator. ive also tried if previousGear == (2) || (3) ledmatrix.clear(); and I get error message "previousGear" cannot be used as a function.

Could you please elaborate a little more? And again, Thank you for the response.

It will be easier to provide help if you post what you have tried.

Try this:

ledmatrix.setTextColor(50, 0);

I suspect that without that extra ", 0" the print() function writes only the pixels needed to display the character, and does not clear the other pixels.

PaulRB,
You are awesome!!!! I haven't messed with it in a few days because of other things and I jopped on today and saw your suggestion, tried it and it works flawlessly now and doesn't overwrite the characters. Thank you, Thank you, thank you.