 # 3 digit numbers input on a keypad

Hi All;

I can get a 1 digit number out of a keypad with ease (as can many). but would really like to get a 3 digit number from the keypad.

Reading around on the interwebs has given me MANY different posts of trying to do this but I am really struggling to get my head around the way you do this.

My current thoughts go something along the lines of

Having a count of the number of digits entered; and using this number to define which variable the keypress is stored under.

Having to press a key to enter the values. Which would then read the values of the 3 variables sequentially and save the 3 digit number as a new 3 digit long variable.

start with a cleared variable of unsigned short digits loop 3 read digit digits = digits * 10 + digit end_loop

digits contains integer number

So what your suggesting is to basically type a number '1' then when the second digit is pressed times the variable by ten and add '2' to give '12' then for the 3rd digit repeat to give '123'. All with 1 variable?

Given your requirements above I don’t see why not

Totally Glad i asked before embarking on the method i was going to try.

A 3 digit decimal number can be max 999 which is to big for a uint8_t but won't overflow a uint16_t nor have you said anything concerning negative values, so ...

remember 0 * 10 + digit means no needing to treat first vs second numbers differently but you didn't say anything about leading zeros so maybe won't work for you

Right so have just tried this out in a separate project to my main one for testing purposes. Was just for this test trying to save a variable and then multiply that variable by 10.

Compiles fine; the oddity is that the digits variable always reads between 480 for 010 and
570 for 9
10

confused as to why this might be.

Andrew

``````*/
int Value;
int digits;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'D','C','B','A'},
{'#','9','6','3'},
{'0','8','5','2'},
{'*','7','4','1'}
};
byte rowPins[ROWS] = {3, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad

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

void loop(){
digits = customKey*10;

if (customKey){
Serial.println(customKey);
Serial.println(digits);
}
}
``````

Did you forget to translate the ‘key’ from ascii to integer?

duh...

Cheers

Andrew

Sorry to be such a pain the way I got round this in the past (stupid I know) is to minus 48 from the read value.

This is causing me issues (unsurprisingly)

What is the best way of doing this in your opinion...

Well, if it's 0 ...9

digit = digit - '0'

or

digit -= '0'

``````#include <ctype.h>

const uint8_t ROWS  = 4;
const uint8_t COLS  = 4;

char hexaKeys[ROWS][COLS] =
{
{ 'D', 'C', 'B', 'A' }
, { '#', '9', '6', '3' }
, { '0', '8', '5', '2' }
, { '*', '7', '4', '1' }
};

byte rowPins[ROWS]  = {  3,  6,  7,  8 };
byte colPins[COLS]  = {  9, 10, 11, 12 };

int digits          = 0;

void loop()
{
if ( isdigit(ch) )
{
uint8_t digit   = ch - '0';
digits          = ((digits * 10) + digit);

Serial.println(ch);
Serial.println(digits);
}
}

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