Store key pressed data in variable

Hello, There !

In one of the my project I am trying to save the data of key pressed (3x4 Keypad) in variable using arduino mega2560 but unable to do that. I want to store data like, if i pressed "4356789" that data will store in one variable and then i will work on that variable. For the testing purpose i am trying to display it on LCD . Can anyone tell me how to do that?

Source code of keypad:

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

const byte ROWS = 4; 
const byte COLS = 3; 
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'-','0','+'}
};

byte rowPins[ROWS] = {29,30,31,32}; 
byte colPins[COLS] = {26,27,28}; 

LiquidCrystal lcd(38,37,36,35,34,33);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  lcd.begin(16, 4);
  lcd.setCursor(0,0);
}

void loop() {
char key = keypad.getKey();

    if (key) {
        lcd.print(key);
    }
}

Study serial input basics. I believe it could be bent to your purpose.

Welcome,

Here is an example

void loop()
{
  const char key = keypad.getKey();
  
  if (key)
  {
    static uint32_t number = 0;
    
    if (key >= '0' && key <= '9') // if it's a digit, add it to number
    {
      number = (number * 10) + (key - '0');
    }
    else if (key == '-') // - to reset number
    {
      number = 0;
    }
    else if (key == '+') // + to validate number
    {
      Serial.print("You entered number ");
      Serial.println(number);
      number = 0;
    }
  }
}

See Keypad data entry

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