Input and storing a number from Keypad

Hello,I need some help
I would like to store a variable that i will be typing with a 4x3 keypad and then print it in a Lcd display and i don’t know how.
I have noted in the code below were I’d like to do that.
Thank you in advance!

#include <Keypad.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(14, 15, 16, 17, 18);

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] = {9, 7, 6, 5};
byte colPins[COLS] = {4, 3, 10};

int count = 0;

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


void setup()

{

  Serial.begin(9600);
  display.begin();
  display.setContrast(50);

  display.display(); 
  delay(2000);
  display.clearDisplay();  


  while (!Serial) {
    ;
  }
  display.setTextColor(BLACK);
  display.setCursor(0, 0);
  display.setTextSize(1); 
  display.println(F("Hello")); 
  display.println(F("To enter a number press #"));
  display.display();
  delay(1000);
}


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

  if (key != NO_KEY)
  {
    display.setTextSize(1);
    display.print(key); 

    if (key == '#')
    {
      display.clearDisplay();
      display.print(F("Press a number:"));
      display.display(); //here i want to store a 10digit number that i will be typing
    }
    if (key == '*')
    {
      display.clearDisplay();
      display.print(F("Number:"));
      display.println();//print the stored number
    }
      if (key == '0')
      {
        display.clearDisplay();
        display.print(F("Cancel"));

      }
    
    display.display();
    delay(2000);
  }
}

If I read off a series of numbers, like 1,6,2, what number would you come up with, and how?

The first digit gets stored as a number. Here's a hint: '4' - '0' = 4. So, the number is 1.

The next digit, '6' - '0', is entered. How do you transform the 1 and the 6? What number do you get?

I want to store a 10digit number like 0123456789 , which i will be entering from the keypad ,in a variable. I want to use this variable later in the code in order to print it on the Lcd.

The getKey() method returns a character. How do you want to store the value that you entered? As an array of characters?

If so, you declare an array if eleven characters.

char userinput[11];

Next, every keypress can be stored in one element of the array. Use a counter to indicate where the keypress must be written; after storing one keypress in the array, increment the counter. The eleventh element (numbered 10 because array index starts at 0) must be set to '\0'.

Read up on arrays, try something and tell us if you get stuck and where.

Question: with your current code, how can I enter a number like 1000000000 ? You have given a special meaning to '0'.

I made some changes in the code but still I can’t get the proper result. I have declared char userinput[11].
I would appreciate any further help.

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

  if (key != NO_KEY)
  {
    display.setTextSize(1);
    display.print(key);

    if (key == '#')
    {
      display.clearDisplay();
      display.print(F("Press a number:"));
      display.println();;  //here i want to store a 10digit number that i will be typing

      for (count = 0; count < 11; count++)
      {
        userinput[count] = key;
      }

    }
    if (key == '*')
    {
      display.clearDisplay();
      display.print(F("Number:"));
      display.println(userinput);//print the stored number
    }


    display.display();
    delay(2000);
  }
}
[\code]

You don’t store at the right place. With your approach, you will have to read the key again

      for (count = 0; count < 11; count++)
      {
        userinput[count] = key;  <-------------- stores the current key in the array.
      }

You can change ‘key’ to ‘keypad.getKey()’ but that makes life complicated as you have to test for NO_KEY, ‘*’ and ‘#’ again.
The below might be a better way.

void loop()
{
  // user input array; 10 digits and nul character
  static char userinput[11]
  // variable to remember where in array we will store digit
  static int count = 0;

  char key = keypad.getKey();

  if (key != NO_KEY)
  {
    display.setTextSize(1);
    display.print(key);
  }

  switch (key)
  {
    case NO_KEY:
      // nothing to do
      break;
    case '#':
      display.clearDisplay();
      display.print(F("Press a number:"));
      // clear the current user input
      memset(userinput, 0, sizeof(userinput));
      // reset the counter
      count = 0;
      break;
    case '*':
      display.clearDisplay();
      display.print(F("Number:"));
      display.println(userinput);//print the stored number
      break;
    default:
      // if not 10 characters yet
      if (count < 10)
      {
        // add key to userinput array and increment counter
        userinput[count++] = key;
      }
      break;
  }

  display.display();
  delay(2000);
}

Note that I made userinput and count local to loop(); remove them from wherever you had declared them. Because both variables are static, their content will not be lost between successive calls to loop. They act from that perspective as global variables but only have loocal scope so other functions can’t modify them.

Your if statements are basically replaced by a switch / case. To be able to display the pressed key, I had to implement one if, else I had to add the display of the pressed key to each case.

The switch has three cases and a default case. I assume that case NO_KEY, case ‘*’ and case ‘#’ are self explaining. The default case catches all other values, which for the keypad, should be the numeric keys.

The default case checks if less than 10 digits are entered; if so, it adds to the userinpput array at the position indicated by count and increment count.

Hope this gets you on the way.