Need help with backspacing with a 3x4 keypad

Hello,

Im currently working on a project to control a steppermotor(23) with a lcd and a 3x4 keypad.

The problem im getting is that my backspace is not working properly.

The code is not finished yet but im testing it every step i take. Currently my code is looking like this and there is a error between the lines: 137 -> 164 (void deletenumber)

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
//////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;
volatile int firstnumber=99;  // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
  
/////////////////////////////////////////////////////////////////////
const byte ROWS = 4; // Vier rijen
const byte COLS = 3; // 3 kollomen
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {28, 26, 24, 22}; // Arduino pins naar de 4 rijen
byte colPins[COLS] = {35, 33, 31}; // Arduino pins naar de 3 kollomen
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition

////////////////////////////////////////////////////////////////////// 

void setup() {
  lcd.init();                   // initialiseer het LCD scherm
  lcd.backlight();              // zet de backlight aan

  lcd.clear();                  // wis het scherm
  lcd.setCursor(0, 0);          // zet de cursor op positie 1, regel 1
  lcd.print("Huidige  maat|");    // schrijf op scherm
  lcd.setCursor(0, 1);
  lcd.print("-------------+------");   
  lcd.setCursor(0, 2);          
  lcd.print("Gewenste maat|");
  lcd.setCursor(0, 3);
  lcd.print("--------------------");   
  lcd.setCursor(18, 0);         
  lcd.print("MM");              
  lcd.setCursor(18, 2);         
  lcd.print("MM");              

//////////////////////////////////////////////////////////////////////

  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
}
 
void loop() {
   char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY){  // If keypad button pressed check which key it was
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;

      case '3':
        checknumber(3);
      break;

      case '4':
        checknumber(4);
      break;

      case '5':
        checknumber(5);
      break;

      case '6':
        checknumber(6);
      break;

      case '7':
        checknumber(7);
      break;

      case '8':
        checknumber(8);
      break;

      case '9':
        checknumber(9);
      break;

      case '0':
        checknumber(0);
      break;

      case '*':
        deletenumber();
      break;

    }
  }
} 
void checknumber(int x){
  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber=x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
      lcd.setCursor(14, 2);         
      lcd.print(displayvalue);
    
  } else {
    if (secondnumber == 99) {  // Check if it's the 2nd number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      lcd.setCursor(14, 2);         
      lcd.print(displayvalue);
      
    } else {  // Check if the 3rd number entered
      if (thirdnumber == 99) {
         thirdnumber=x;  
         String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
         lcd.setCursor(14, 2);         
         lcd.print(displayvalue);
     } else {  // It must be the 4rd number entered
      fourthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
         lcd.setCursor(14, 2);         
         lcd.print(displayvalue);
      }
    }
  }
}

void deletenumber() {  // Used to backspace entered numbers
 if (fourthnumber !=99) {
     String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
     lcd.setCursor (14, 2);
     lcd.print(displayvalue);
     fourthnumber=99;
  }
  else
   if (thirdnumber !=99) {
       String displayvalue = (String(firstnumber) + String(secondnumber));
        lcd.setCursor(14, 2);         
        lcd.print(displayvalue);
        thirdnumber=99;
   } 
   else {
     if (secondnumber !=99) {
       String displayvalue = String(firstnumber);
       lcd.setCursor(14, 2);
       lcd.print(displayvalue);
       secondnumber=99;
    } 
    else {
      if (firstnumber !=99) {
        String displayvalue = " ";
        lcd.setCursor(14, 2);
        lcd.print(displayvalue);
        firstnumber=99;
       } 
    }
  }
}

You should describe in detail what behaviour of the code you observe and what behaviour you want to have

best regards Stefan

allright what i want to achieve is this:

I want to enter 2650 in my keypad. But i accidently pressed the wrong button so now my lcd says 2655 for example. So i want to backspace the last digit using the (.) on the keypad. but it doesnt it doesnt work. it only works when i want to backspace the first digit.
for example:
I press 1 on the keypad. i press (.
) after. now the 1 removes like its supossed to but when ever i press 11 for example and i try to backspace it won't work. when i press backspace again it wil remove the first digit but the second digit stays at the same place.

Do you know how to solve this?

Thanks for your time

still not precise enough
You enter a single digit and backspace works
I don't understand which key this strange () is

char keys[ROWS][COLS] = {
  {'1','2','3'}, // which key is it?
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

if you entered two digits like "11" and you press backspace what do you observe on the display?

best regards Stefan

The key im trying to type wont show up in the commands its the star symbol (bottom left of the char keys)

if i enter 11 it will show [11 ]
when i press backspace it will show [11 ]
when i press back space again it will show [ 1 ]
its hard to explain like this but it removes the first digit and leaves the second one where it is

if i enter 37 it will show [37 ]
when i press backspace it will show [37 ]
when i press back space again it will show [ 7 ]

it becomes more precise by using two different digits like 37
is this correct? The second digit the 7 stays there
and if you press backspace again and again the "7" stays there forever?

The basic idea of your code is: you initialise four variables with 99 to indicate the digits are not yet typed in on the keyboard
then you press a digit on the keyboard and check if the corresponding variable has value 99
if it has value 99 you assign the pressed digit

Me personal I would use this approach:
if you want to enter 4-digits numbers like 1234, 2560, etc. there is one single array of char with 5 elements. You need 5 elements because a char-array must always be terminated with value 0 for indicating end-of-string

Arrays start at index 0. This means a 5-element array has the index-numbers

myCharArray[0]
myCharArray[1]
myCharArray[2]
myCharArray[3]
myCharArray[4]

each time you press a digit on your keyboard the digit is assigned to an array-element
and the index-number is increased by one

first press is a "5"
myCharArray[0] = '5';
and additionally to terminate the string
myCharArray[1] = 0;

second press is a "6"
myCharArray[1] = '6';
and additionally to terminate the string
myCharArray[2] = 0;

third press is a "8"
myCharArray[2] = '8';
and additionally to terminate the string
myCharArray[3] = 0;

fourth press is a "9"
myCharArray[3] = '9';
and additionally to terminate the string
myCharArray[4] = 0;

and on the display you simple print

lcd.print(myCharArray);

if you use backspace
the last entered digit gets assigned 0
example
you entered three digits

568
corresponding to
myCharArray[0] contains a 5
myCharArray[1] contains a 6
myCharArray[2] contains a 8

last valid digit has index-number 2

you press backspace one time
and you execute

myCharArray[2] = 0; 

and you are done because the zero indicate end-of-string
For later use you convert the array-of char to an integer with function
atoi()
which also uses the terminating zero for finding the digits

best regards Stefan

i think this is simpler

#undef MyHW
#ifdef MyHW
# include "sim.hh"
#else
# include <Keypad.h>
# include <Wire.h>
# include <LiquidCrystal_I2C.h>
#endif

//////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd (0x27, 20, 4);
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;

unsigned long number;

/////////////////////////////////////////////////////////////////////
const byte ROWS = 4; // Vier rijen
const byte COLS = 3; // 3 kollomen
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {28, 26, 24, 22}; // Arduino pins naar de 4 rijen
byte colPins[COLS] = {35, 33, 31}; // Arduino pins naar de 3 kollomen
Keypad keypad = Keypad ( makeKeymap (keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition

//////////////////////////////////////////////////////////////////////

void setup () {
    lcd.init ();                   // initialiseer het LCD scherm
    lcd.backlight ();              // zet de backlight aan

    lcd.clear ();                  // wis het scherm
    lcd.setCursor (0, 0);          // zet de cursor op positie 1, regel 1
    lcd.print ("Huidige  maat|");    // schrijf op scherm
    lcd.setCursor (0, 1);
    lcd.print ("-------------+------");
    lcd.setCursor (0, 2);
    lcd.print ("Gewenste maat|");
    lcd.setCursor (0, 3);
    lcd.print ("--------------------");
    lcd.setCursor (18, 0);
    lcd.print ("MM");
    lcd.setCursor (18, 2);
    lcd.print ("MM");

    //////////////////////////////////////////////////////////////////////

    pinMode (stepPin,OUTPUT);
    pinMode (dirPin,OUTPUT);
    pinMode (enPin,OUTPUT);
    digitalWrite (enPin,LOW);
}


void loop () {
    char key = keypad.getKey ();

    switch (key) {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
        number = (10*number) % 1000 + key-'0';
        lcd.setCursor (14, 2);
        lcd.print (number);
        break;

    case '*':
        number /= 10;
        lcd.setCursor (14, 2);
        lcd.print (number);
        break;

    case NO_KEY:
        break;
    }
}