Backspace doesnt function properly (coding keyboard)

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
int buttonA= 2;
int buttonEnter= 3;
int buttonB= 4;
int buttonB1= 5;
int buttonC=6;
int seconds = 0;
int row=0;
int cr1=0;
int cr2=0;
boolean bsp = false;
LiquidCrystal_I2C lcd(0x3F,16,2);
void setup()
{
 lcd.init(); 

    lcd.backlight();
  Serial.begin(9600);
    pinMode(buttonA,INPUT_PULLUP);
    pinMode(buttonB1,INPUT_PULLUP);
    pinMode(buttonEnter,INPUT_PULLUP); //button
    pinMode(buttonC,INPUT_PULLUP);
  pinMode(buttonB,INPUT_PULLUP); 
 lcd.setCursor(0,0);

}

void loop()
{

   if(digitalRead(buttonB)==LOW){
         lcd.rightToLeft();
        if (row==0){
          lcd.print(" ");
          cr1=cr1-1;
           delay(100);
        }
        else if (row==1){
          lcd.print(" ");
          cr2=cr2-1;
          delay(100);
  }
  if (digitalRead(buttonB)!=LOW){
   
     if (row==0){
      lcd.setCursor(cr1++,row);  
      }
     else if (row==1){
      lcd.setCursor(cr2++,row);
        }
        bsp=false;
      }
          lcd.leftToRight();
     }
   

   if(digitalRead(buttonC)==LOW){
     lcd.print("c"); 
      delay(100);
    if (row==0){ cr1=cr1+1;
               }
    else if (row==1){
      cr2=cr2+1;
    }
  }
  if(digitalRead(buttonB1)==LOW){
     lcd.print("b");
          delay(100);
    if (row==0){ cr1=cr1+1;
               }
    else if (row==1){
      cr2=cr2+1;
    }
  }
  if(digitalRead(buttonA)==LOW){ 
     lcd.print("a"); 
          delay(100);
    if (row==0){ cr1=cr1+1;
               }
    else if (row==1){
      cr2=cr2+1;
    }
  }
     


   if(digitalRead(buttonEnter)==LOW){ 
     
          
     if (row==0){
      row=row+1;
       lcd.setCursor(cr2,row);
      delay(300); 

      }
     
     else if (row==1){
      row=row-1; 
      lcd.setCursor(cr1,0);
      delay(300); 
        }
 
  }

  
}

so im coding a keyboard and so far have a,b,c keys, an enter key (all of which work good) and then the back space key. today is the furthest ive gotten on it, where it works perfectly after first use. but for example if i type "abc" then delete the "c" the next letter input will overwrite the "b" (previous column). this would be an easy fix but it ONLY happens after first use, on the second time using backspace it writes to the correct column on the lcd.

can anybody point out why this might be happening and a possible fix?

button B is backspace
button B1 is letter b ( i know dumb variable names)
row keeps track of row
cr1 keeps track of place on column for row one
cr2 does that for row 2.

  if(digitalRead(buttonB) == LOW) {
    if (row == 0) {
      lcd.setCursor(--cr1, row); 
      lcd.print(" ");
      }
    else if (row == 1) {
      lcd.setCursor(--cr2, row); 
      lcd.print(" ");
      }
   delay(100);
  }
1 Like

Even better:

  if(digitalRead(buttonB) == LOW) {
    if (row == 0) {
      lcd.setCursor(--cr1, row); 
      }
    else if (row == 1) {
      lcd.setCursor(--cr2, row); 
      }
   lcd.print(" ");
   delay(100);
  }

Still even better:

int cursor[2];  // instead of cr1 and cr2

  if(digitalRead(buttonB) == LOW) {
    lcd.setCursor(--(cursor[row]), row); 
    lcd.print(" ");
    delay(100);
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.