Keypad 4x4 key 'B' coding issue

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

int R = 0;
int C = 0;
int R1 = 1;
int C1 = 1;

int patch=1;

byte pin_rows[ROW_NUM] = {A15, A14, A13, A12}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {A11, A10, A9, A8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

// #define Single_long_click_duration 10000

void setup() {
  lcd.init(); // display initialization
  lcd.clear(); // clear the screen fully
  lcd.backlight(); // activate the backlight
}

void loop()
{
  lcd.setCursor(C1, R1);
  char key = keypad.getKey();
  if (key)
  {
    switch(key)
    {
    case '1':
    lcd.print(key);//something
    C1++;
    if(C1>3)
    {
      C1=3;
    }
    break;

    case 'B':
    if(C1>1)
    {
    C1--;
    if(C1<1)
    C1=1;
    lcd.setCursor(C1,R1);
    lcd.print(' ');
    }
    break;
}
}

Here I am using Key 'B' as backspace function to erase the content of key '1'

key '1' is coded in a way to print up to 3 places maximum from (1,1) to (3,1) location as observed in code above

For single digit '1' at (1,1) & also for double digit '1' it works properly

But for 111 its erasing firstly '1' at (2,1) and then '1' at (1,1) but don't use to effect '1' at (3,1) location

Hard to pinpoint what You're trying to tell. Here's a guess.
Positions on a 16 by 2 char lcd start with (0, 0) in the upper left corner. 0, 0 to 15, 0 is the upper line. 0, 1 to 15, 1 is the lower line.

Ya I am using the second Row 2nd position means (1,1) as the starting point

From that point when we use to press '1' it start printing up to location (3,1)
Just 3 positions to take input as maximum

Now using backspace function with key 'B' :
When we use to press it , the digit should clear the printed 111 in opposite direction one by one

From position (3,1) to (2,1) to (1,1) and complete black out there

But I am facing an issue here =>
with 3 digits 111 , when we use to press key B
It starts from (2,1) to (1,1) and complete blank

and the 1 at position (3,1) remains there
No Change

You can check the function with code in WOKWI

The C, and/or C1 is the reason. Use Serial.print and serial monitor to se what's happening in the code.

When you press 111, we see 111.
When you press BBB, we see (space)(space)1

You need to start "B" to the "right" of C1 (C1+1)
You only needed to move the lcd.setCursor and lcd.print above the C1 code.

      case 'B':
        Serial.print(C1); // from @Railroader
        if (C1 > 0)
        {
          lcd.setCursor(C1, R1);
          lcd.print(' ');
          C1--;
          if (C1 < 1)
            C1 = 1;
        }
        break;

Ya started !!

Ya that is working !!!
Great Sir !!
Thanks :blush:

1 Like

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