 # Keypad Input 2 digit numbers

Hello!
I am pretty new to programming and I need help. I’m making a scoreboard that will be controlled by a keypad. To change the score, you will enter the number (1-15, which is the max you are able to score), and then press either A or B to select the team that the points will go to. Currently in my code, when I enter the first number, ex: 1, it outputs as 11. Any help would be greatly appreciated!!!

``````#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

int x;
int y;
int scoreA;                           // Team A
int scoreB;                           // Team B
int scoreAdd;            // Hold the number that will be added to the team and the number that will be subtracted from current score if over 21

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

scoreA = 0;
scoreB = 0;
x = 0;
y = 0;
}

void loop() {

//  if (scoreA > 21) {
//    scoreAdd -= (21 - scoreA);
//IGNORE ME            //    scoreA -= scoreAdd;
//  }

if (key){
Serial.println(scoreA);
//Serial.println(scoreB);
}

if (key != NO_KEY) {
if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9') {
x = key - '0';
delay (500);
if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9') {
y = 10 * (key - '0');
}
}
if (key == 'A') {
x = 0;
y = 0;
}
if (key == 'B') {
x = 0;
y = 0;
}
}
}
``````

You’re adding the key’s value and then immediately 10* the same key’s value in the same “breath” so to speak.

Have a look at the function below, where each key press is only used once, and you can accumulate as many digits as you need, until a non-digit is pressed.

It only multiplies the existing value-so-far by 10 just before adding the new value.

So if you press 1 to start, it adds 1 to value, to get 1 so far. If you then press a 3, it multiplies the existing 1 x 10 to get 10, then adds the 3 for 13.

Call the function like this:

``````int myValue = getKeypadInteger();
``````

Here’s the function:

``````int getKeypadInteger()
{
int value = 0;                                // the number accumulator
int keyval;                                     // the key press
int isnum;
Serial.println("Enter digits, any non-digit to end ");
Serial.print("So far: ");
do
{
keyval = keypad.getKey();                          // input the key
isnum = (keyval >= '0' && keyval <= '9');         // is it a digit?
if (isnum)
{
Serial.print(keyval - '0');
value = value * 10 + keyval - '0';               // accumulate the input number
}

} while (isnum || !keyval);                          // until not a digit or while no key pressed
Serial.println(" "); Serial.print("Returning "); Serial.println(value);
return value;

``````

edit: “somewhere” in the forum I recall seeing a better version of similar code to mine, that allows one of the non-digit keys to be a backspace, to take out a digit if you mis-type.

Thank you so much! I got it working, Here is my final code for this segment.

``````#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

int scoreA;                                        // Team A
int scoreB;                                        // Team B
int scoreAdd = 0;                                  // Hold the number that will be added to the team and the number that will be subtracted from current score if over 21
int value = 0;                                     // the number accumulator
int key;                                           // the key press
int isnum;

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

scoreA = 0;
scoreB = 0;
}

void loop() {

do
{
key = keypad.getKey();                          // input the key
isnum = (key >= '0' && key <= '9');             // is it a digit?
if (isnum)
{
scoreAdd = scoreAdd * 10 + key - '0';         // accumulate the input number
}

} while (isnum || !key);                          // until not a digit or while no key pressed

if (key != NO_KEY) {
if (key == 'A') {
if (scoreA > 21) {
}
Serial.println(scoreA);
Serial.println(scoreB);
}
if (key == 'B') {
if (scoreB > 21) {
}
Serial.println(scoreA);
Serial.println(scoreB);
}
}