Here is an example (I don't have a keypad to hand right now so this is untested) for the 4x4 membrane keypad (HCPROJ0001) we sell for how to convert the row/col value returned by the read() function to the actual value printed on its keys:
Note, the key2val() function may look a bit long winded but as you're only mapping 10 keys it's actually more memory efficient to do it this way then have a lookup table and a loop function...
- void setup()
- {
- Serial.begin(9600);
- }
- /* Main program */
- void loop()
- {
- Keypad.Scan();
- if(Keypad.New_Key())
- {
- byte keyVal = key2val(Keypad.Read());
- if(keyVal != 255)
- Serial.println(keyVal);
- }
- }
- // Convert row/col index to key value
- byte key2val(byte key)
- {
- byte val = 255;
- if(key == 11)
- return 1;
- if(key == 12)
- return 2;
- if(key == 13)
- return 3;
- if(key == 21)
- return 4;
- if(key == 22)
- return 5;
- if(key == 23)
- return 6;
- if(key == 31)
- return 7;
- if(key == 32)
- return 8;
- if(key == 33)
- return 9;
- if(key == 42)
- return 0;
- return val;
- }