Pages: [1]   Go Down
Author Topic: Combining digits from the keypad.getKey() function  (Read 1890 times)
0 Members and 1 Guest are viewing this topic.
South East USA
Offline Offline
God Member
*****
Karma: 5
Posts: 655
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

...
Code:
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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

South East USA
Offline Offline
God Member
*****
Karma: 5
Posts: 655
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

All the best!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,'#'}
};
« Last Edit: April 06, 2011, 01:07:54 pm by robtillaart » Logged

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: