input and save up to 3 digit dec # using Uno and 4x4 matrix keypad

I wan to add the capability to enter and store a decimal variable, up to 3 digits, 0 to 999, using a 4x4 matrix keypad. I have the keypad connected to my Uno and reading the keys, but it is only displaying the individual keypress. I want to create a variable x and enter 1 to 3 digits and store it. I have tried to search for examples but have not found any.


One way.

Read the first digit, multiply it by 100 and assign it to the variable. Read the second digit, multiply it by 10 and add it to the variable. Read the third digit and add it to the variable.

Make sure you know what you are reading because they are likely to be chars, ie a representation of the number, rather than the number itself. If so, subtract '0' from the value read to convert to the real number before doing each step above.

Thanks, I'll try that.

Any good examples of code would help...


This has the same concept you can use.

I found an old post here with code that seems to work. With just the Uno and the keypad, connected as in another sketch Iā€™m working on, the serial monitor displsys the input #, but the stored value is different. I tried subtracting ā€˜0ā€™ as was advised in the original post, but the stored value is still different. I do not understand.

Here is the serial monitor ouput for entries of 1, 12, and 123.

Here is my code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
byte rowPins[ROWS] = {11,10,9,8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7,6,5,4}; //connect to the column pinouts of the keypad

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

void setup(){

void loop(){
  char key = keypad.getKey();              
  if (key != NO_KEY && key != '#') 
  Serial.print (key);   //prints input digit
     for (int i = 3; i != 0; i--) {              //shifts digits to left
     value = value * 10 + key - '0';
} if (key == '#'){                               //prints value 
      Serial.println ();
      Serial.print (value);
      Serial.println ();}
      value = 0;

Where is the -1443 coming from and why is it not changing for the 3 different inputs. Please explain as I do not understand.