Help with keypad!

Good day everyone!
I need to know how to enter double digits onto a lcd . For now i have no problem entering from digits 0-9, but what do i need to do in order to enter the digit 10> ?

If i enter 1 , then 0, it will just change accordingly on the lcd, 1, then 0. Thanks for all your help in advance :slight_smile:

Think about this for a moment. The computer really can't know unless you have some way to tell it that the number is complete. Like a # key or a timeout.

You say that you already have it working for single digits.

Please post the code you already have, which works for single digits. Then maybe we can help you figure out how to modify it for double digits.

Multiply what you have by ten and add the new digit.

This is my code. ignore the bottom part , i just want to show you how i code they keypad with the LCD. Thanks!

fyp2.ino (1.38 KB)

OP’s code formatted and inlined.

You’d be surprised how many bugs you’ll find just formatting code. There’s a reason we do it with the indenting and the blocks all lined up a certain way and it isn’t just because it’s pretty.

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

const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
byte rowPins[rows] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad left first 4
byte colPins[cols] = {6, 7, 8}; //connect to the column pinouts of the keypad right last 3
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
const int rs = A7, en = A2, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int i = 0;
int led = 14;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode( led, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0, 0);
  lcd.print("Select item");
  lcd.display();
  char key = keypad.getKey ();
  if (key != NO_KEY) {

    Serial.println(key);
    lcd.setCursor (0, 1);
    lcd.print(key);
    lcd.display();
  }

  if (key != NO_KEY)

  {
    switch (key)

    {
      case '1':
        digitalWrite(led, HIGH);
        delay(1000);
        digitalWrite(led, LOW);
        lcd.clear();
        break;

      case '2':
        digitalWrite(led, HIGH);
        delay(500);
        digitalWrite(led, LOW);
        delay(500);
        lcd.clear();
        break;

    }

    lcd.clear();
    lcd.setCursor (0, 0);
    lcd.print("Enter Amount");
    lcd.display();
    lcd.setCursor (0, 1);
    lcd.print(key);
    lcd.display();
  }
}

Instead of just printing key to the display, create yourself a variable and use key to increase or decrease it as you will and then display that variable to the display.

Delta_G:
Multiply what you have by ten and add the new digit.

int myValue = 0;


////  in loop

// get a key from the keypad

key -= 48;  //  Because you have ascii codes instead of number in the matrix

myValue = (myValue * 10) + key;   // add key digit to the end of myValue.

Hi delta, thanks for the fast response!
Im just wondering why is key-=48?

bob112112:
Hi delta, thanks for the fast response!
Im just wondering why is key-=48?

I told you right in the comment next to it.

In your matrix your first button doesn't give 1, it gives '1' which is equal to 49. http://www.asciitable.com/

Thank you so much ! On a side note, how do i enter store the value i want now? For example , if i want to store the value 20, how do i " Enter " it as a variable and store it? Thanks in advance!

You write a code that checks for some other key that isn't a digit, like # or something and if it is that key then instead of adding it to your digit you mark that your number is complete.