Coding an LCD to show 7 keypad inputs consecutively

How can I code a Arduino Uno, a 3x4 keypad, and a module 1602 LCD to display 7 consecutive key inputs at the same time? This is the code I already have:

#include <Keypad.h>
#include <LiquidCrystal.h>
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd( RS, EN, D4, D5, D6, D7);
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
char keys[4][3] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[4] = {13, A0, A1, A2}; //connect to the row pinouts of the keypad
byte colPins[3] = {A3, A4, A5}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(void) {
 lcd.begin(16, 2); // Initialize the LCD
  lcd.setCursor(0, 0);
  lcd.print("Press a key:");
}
void loop(void) {
 char key = keypad.getKey();
  
  if (key){
    lcd.setCursor(0, 1); // set cursor to second line
    lcd.print("Pressed Key: ");
    lcd.print(key);

So what is the problem ?

I want the LCD to display multiple keypad inputs, but it currently only displays one at a time.

Then stop putting the cursor back to 0, 1 before printing a character

oh.

Have you got it working now ?

for the most part of what i need it to do.

Here is a small exercise for you

Change the sketch to print the characters that you enter from right to left across the screen

Your code posted is incomplete.
Please post the complete code.

Hi @anon25057619 ,

here is something to play with:

/*
  Forum: https://forum.arduino.cc/t/coding-an-lcd-to-show-7-keypad-inputs-consecutively/1228775
  Wokwi: https://wokwi.com/projects/390821877491852289
*/


#include <Keypad.h>
#include <LiquidCrystal.h>
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd( RS, EN, D4, D5, D6, D7);
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
char keys[4][3] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[4] = {13, A0, A1, A2}; //connect to the row pinouts of the keypad
byte colPins[3] = {A3, A4, A5}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const byte bufLen = 7;
byte index = 0;
char buf[bufLen + 1] = "";

void setup(void) {
  Serial.begin(115200);
  lcd.begin(16, 2); // Initialize the LCD
  lcd.setCursor(0, 0);
  lcd.print("Press a key:");
}
void loop(void) {
  evaluateKey(keypad.getKey());
}

void evaluateKey(char aKey) {
  if (!aKey) {  // If no key pressed return immediately
    return;
  }
  switch (aKey) {
    case '*' :  // Clear second line and key buffer
      resetBuffer();
      break;
    case '#' :  // Revert sequence in buffer
      invertBuffer();
      break;
    default:    // add all other keys to sequence in buffer until buffer full
      addToBuffer(aKey);
      break;
  }
}


void printBuffer() {
  lcd.setCursor(0, 1); // set cursor to second line
  lcd.print(buf);      // print buffer content
}

void addToBuffer(char newKey) { // add newKey to buffer
  if (index < bufLen) {         // but only if index is less than the available buffer size for keys
    buf[index] = newKey;        // Store in buffer at "index"
    buf[index + 1] = 0x00;      // Store a zero in the following char so that print knows where the string ends
    index++;                    // Increment index 
    printBuffer();              // print the updated buffer
  } else {
    lcd.setCursor(0, 0);        // set cursor to second line
    lcd.print("Buffer full!"); // Print sufficient white spaces to clear the line
  }
}


void resetBuffer() {          // Resetting buffer and clear the second line
  index = 0;                  // Resetting the index is sufficient
  lcd.setCursor(0, 0);
  lcd.print("Press a key:");
  lcd.setCursor(0, 1);        // set cursor to second line
  lcd.print("                 "); // Print sufficient white spaces to clear the line
}

void invertBuffer() {        // Invert the sequence of characters in the buffer
  int half;                  // A variable to guide us halfway through the buffer 
  if (index > 1) {           // Inverting makes only sense if there is more than one character
    half = index / 2;
    for (int i = 0; i < half; i++) {
      char c = buf[i];
      buf[i] = buf[index - 1 - i];
      buf[index - 1 - i] = c;
    }
    printBuffer();
  }
}

You can check it out on Wokwi: https://wokwi.com/projects/390821877491852289

  • Input of 7 characters store in a buffer
  • The key '*' clears the buffer
  • The key '#' inverts the sequence in the buffer
  • "Buffer full" message displayed

Have fun!
ec2021

There may be a language problem here?

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