Go Down

Topic: Combining digits from the keypad.getKey() function (Read 2304 times) previous topic - next topic

SouthernAtHeart

Keypad matrix, using the library:
http://arduino.cc/playground/Code/Keypad

...
Code: [Select]

void loop(){
  char key = keypad.getKey();
 
  if (key != NO_KEY) {
    Serial.println(key);
  }
}


If the user wanted to input the number 10, how would I 'get' that number from the two keypresses '1' and '0'
I haven't written any code yet for the keypad, but here's what I'm thinking:
lcd.print:  "Enter the number of cups of coffee & press #"
     user can enter a number (2,4,6,8,10)
So if they they enter 2 digits, I need to combine them into 1 variable.

CrossRoads

Well, just take the inputs & do some math on it:
before reading clear the prior entries
digit1 = 0
digit0 = 0

after you get the entries
total = 10*(digit1) + digit0


Or, you will have a 4x3 keypad?
Make one of the keys = 10.
1-2-3-4-5-6-7-8-9-10-enter-clear
0 = 10,
# = clear,
* = enter
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

SouthernAtHeart


Well, just take the inputs & do some math on it:
before reading clear the prior entries
digit1 = 0
digit0 = 0

after you get the entries
total = 10*(digit1) + digit0


Or, you will have a 4x3 keypad?
Make one of the keys = 10.
1-2-3-4-5-6-7-8-9-10-enter-clear
0 = 10,
# = clear,
* = enter


Thanks, yes, 3x4,
I see now.  I could do either of these...

YT2095

i`m just wondering about the maths part, as to whether it would still parse as ASCII?
and thus be useless for any Math type functions.
the only reason I`m bringing up this old(ish) thread is that I had a similar problem trying to parse these numbers as REAL ones rather than some arbitrary ASCII equiv.
after a good mess about I discovered that you CAN in fact do this with the keypad Lib, my HEX keymap now looks like:

char keys[ROWS][COLS] = {
  {0x000c,0x000d,0x000e,0x000f},
  {0x0008,0x0009,0x000a,0x000b},
  {0x0004,0x0005,0x0006,0x0007},
  {0x0000,0x0001,0x0002,0x0003}
};


This isn`t the End of my Journey yet, as I still need to then push these out to 1 Byte serial piso 74595`s yet ( 3 of them) 2 for Address and one for Data.
but it`s enough to perhaps help Some along the way ;)

All the best!

robtillaart

#4
Apr 06, 2011, 08:02 pm Last Edit: Apr 06, 2011, 08:07 pm by robtillaart Reason: 1
as char is only one byte big your char map should be

char keys[ROWS][COLS] = {
 {0x0c,0x0d,0x0e,0x0f},
 {0x08,0x09,0x0a,0x0b},
 {0x04,0x05,0x06,0x07},
 {0x00,0x01,0x02,0x03}
};

but better use the chars directly in your array :

char keys[3][4] = {
 {'7','8','9'},
 {'4','5','6'},
 {'1','2','3'},
 {'*'.'0'.'#'}
};

or use the int values you want except for the * and # all is possible.

char keys[3][4] = {
  {7,8,9},
  {4,5,6},
  {1,2,3},
  {'*',0,'#'}
};
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

YT2095

yeah, this is still a bit new(ish) to me, the problem that I`m going to (have already) hit, is concatenating 2 Hex values into a single byte to push out to the Hardware serial array.

I certainly can`t Add them. and I see no Nibble Splice command??????

and my Math is pathetic on a Good day :)

CrossRoads

upper_nibble = upperByte<<4 ; // shifts bits 4 left, leaving 0bXXXX0000 (or whatever the prefix is for BITs)
lower_nibble = 0x0F & lowerByte; // Zero's out upper bits, leaving 0b0000YYYY

combined = lower_nibble | upper_nibble; // ORs together for 0bXXXXYYYY

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy