Numpad mit LCD-I2C 16x2 Display

create an array of sentences you want to see on the LCD

const char * sentences[] = {
"sentence 1", // index = 0
"sentence 2", // index = 1
"sentence 3",
"sentence 4",
"sentence 5",
"sentence 6",
"sentence 7",
"sentence 8",
...
};

create a global variable to remember when the last display was and how long you want it to last as well as a variable that remembers the state of the display

unsigned long lastDisplay;
const unsigned long displayDuration = 20000; // 20s in ms
bool displayActive = false;

Then to make the mapping easy, don't define the keys as such

char keys[ROW_NUM][COLUMN_NUM] = {
  {'D','#','0','*'},
  {'C','9','8','7'},
  {'B','6','5','4'},
  {'A','3','2','1'}
};

but use

char keys[ROW_NUM][COLUMN_NUM] = {
  {'D','#', 1,'*'},
  {'C', 10, 9,  8},
  {'B',  7, 6,  5},
  {'A',  4, 3, 2}
};

this way the key you get when the keypad is pressed is the index+1 you want to use in the sentences array. Don't start the numbering at 0 because that's the value of NO_KEY that is returned when noting is pressed when you call keypad.getKey()

char key = keypad.getKey();
switch(key) {
  case 0: break; // no key
  case 'A': ... break;
  case 'B': ... break;
  case 'C': ... break;
  case 'D': ... break;
  case '*': ... break;
  case '#': ... break;
  default: // then it's the index + 1 in the sentence array
     lcd.setCursor(0,0);
     lcd.print(sentences[key-1]);
    lastDisplay = millis();
    displayActive = true;
    break;
}

you could decide to use also indexes for '#' or '*' or 'B' etc...

then to clear the display after some timeOut, you just do

if (displayActive && (millis() - lastDisplay  >= displayDuration)) {
  lcd.clear();
  displayActive = false;
}
1 Like