How to print input from Matrix Keypad to LCD C1602A-V1.2

Hi there. I’m new to this Arduino programming and I need help. I’m helping Matrix Keypad (4x4) and LCD panel (C1602-V1.2) with me for my project. I tried to use individual coding and combine them together. Unfortunately, I could not get the my keypad print to my lcd panel. Can someone help me with this? Thanks.

This is the coding I tried to combine with:

#include <LiquidCrystal.h>
#include <Keypad.h>

const byte numRows= 4;
const byte numCols= 4;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

char keymap[numRows][numCols]=
{
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[numRows]= {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup(){

lcd.begin(16, 2);
lcd.print(“BUS SERVICE”);
Serial.begin(9600);

}

void loop() {
lcd.setCursor(0,1);
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}

Does the BUS SERVICE message print on the LCD ? If not then I suspect that nothing else will either until you fix that problem.

If it does then why not use lcd.print(keypressed) instead of or as well as Serial.print(keypressed) ? Do you see the value of keypressed in the serial monitor ?

Yes, 'BUS SERVICE' does print on the LCD. Except for the input of keypad doesn't.

Where do I put lcd.print(keypressed) or Serial.print(keypressed) at? Sorry, newbie here.

I did see value of keypressed in serial monitor. However, I want to print it in my LCD as well, which does not reflect in it.

If 'char keypressed = myKeypad.getKey();' returns a number 0 to 9 for example, you wont see anything.

These ascii characters are non-printable (control/status chars).

To see the representation as an integer, you need to cast it to an integer which Serial.print will convert to a string containing the number.

LCD.print( (int) keypressed);

Sorry, I don't get the meaning of what you said. Would you mind type the coding in that section where I can amend it?

I did see value of keypressed in serial monitor. However, I want to print it in my LCD as well, which does not reflect in it.

You currently have

 Serial.print(keypressed);

You need

 Serial.print(keypressed);

and

 lcd.print(keypressed);

UKHeliBob: You currently have

 Serial.print(keypressed);

You need

 Serial.print(keypressed);

and

 lcd.print(keypressed);

I tried to fit lcd.print(keypressed); into my coding. Still, not showing in the lcd.

void loop() { lcd.setCursor(0,1); char keypressed = myKeypad.getKey(); if (keypressed != NO_KEY) { Serial.print(keypressed); lcd.print(keypressed); } }

When I press '6' or 'B', it will cancel off my 'BUS SERVICE' and show blank.

junchenggg: Sorry, I don't get the meaning of what you said. Would you mind type the coding in that section where I can amend it?

Sure, I can provide a simple test case for you.

Here is an example printing a char:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char i = 5;
  Serial.println(i);
}

Nothing useful is printed. char is treated as a single character, not a number. You can see what character the ascii value of 5 will print here: http://www.asciitable.com/. It is a control character and has no printable representation so the Serial monitor basically prints a space for this character.

If 'i' contained the number 65, its ascii representation is the character 'A', and the letter A would be printed.

if you want to see the value of 'i' not represented as a character, but the number itself, you would have to cast it to an int.

Using a cast to represent the char as an int, the print function will display the number contained in the variable, rather than its ascii representation.

void setup() {
  Serial.begin(9600);
}

void loop() {
  char i = 5;
  Serial.println( (int) i);
}

Now it will print the number 5.

For clarification, try each code sample and see!

So which means, I have to do something like this:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char A = 65;
  Serial.println( (int) A);
}

Am I right to say so? Because my keypad is 4 x 4, which comprises of 0 to 9, A to D, * and #. If I want to read either one of the character, I have to input the char as an int.

It does work, when do like this:

void setup() {
  lcd.begin(16, 2);
  lcd.print("BUS SERVICE:");
  Serial.begin(9600);
}

void loop() {
  lcd.setCursor(12, 0);
  char i = 234;
  Serial.println( (int) i);
  lcd.print((int) i);
}

Now I want to have when I use keypad to press and have it print on my LCD panel.

Can I have a sample of how to print character ‘A’ in my lcd panel in void loop? Because I don’t know how to cast it to an int.