This Sketch scans a phone keypad using the Keypad library and detects key pressed, released and held events. The events generate a string that is sent serially denoting the event and key.

 

When a key is pressed, the KeyDown event fires. If it is held longer than a short period, the KeyHolding event is fired (once). It does not continually fire though. (Higher level code could implement that with a timer.)  When the key is released the KeyUp event fires.

Links

 

Device Configuration

As per the previous blog, this sketch targets an Arduino UNO (actually a Freetronics EtherTen). The Arduino UI Serial terminal is used with this app to view the phone keypad key events. The Bluetooth medium is not used for this Sketch (its added in the next one). Pins 4 to 10 are used to connect to matrix keypad. pins 0 & 1are left for debugging and 2 & 3 are reserved for Bluetooth (in the next Sketch).

 

image

The Device and Keypad

 

The Sketch

The Sketch scans a phone matrix keypad using an Arduino library. It captures key press, key up and key hold events. For each event it sends a string to the serial port:

+n,   eg +1    For a key press where n is the typeface on the key ( (0 to 9 or # or *).

-n     eg –1    When the key is released

@n  eg @1   If the key is held (It just sends one string, it doesn’t repeat). Is followed by a
                           release event eventually,

 

The Keypad  library is used as it scans a matrix keypad such as Phone Keypad.

  • Menu->Sketch->Include Library->Manage Libraries
  • Scroll down to Keypad and click on it then select Install.

image

 

My Phone Keypad

This is an off-the-shelf phone keypad. You need to spend some time determining which pins are rows and which are columns; and which rows and which columns they are. I did as a matrix of all pins against all pins with a multimeter and saw which elements in the matrix represented each key. I wrote this all down and then was able to discern the rows and columns:

telphone keypad

My Phone Keypad

This was a little different to that covered the the Keypad Library tutorial.

Note also that the rows and columns are a little “jumbled”.

 

The Code

As per usual, the code consists of declarations, the setup() method and the loop() method. For the uninitiated, the setup() runs once post boot or reset and the Sketch then continuously loops by calling loop()..

#include <Keypad.h>

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


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

//Cols:
//4: KB Pin 3
//5: KB Pin 1
//6: KB Pin 5

//Rows:
//7: KB Pin 2
//8: KB Pin 4
//9: KB Pin 6
//10: KB Pin 7

/* KB Pins:
 *  From left at bottom from above
 *  1,2,3,4,5,6,7
*/

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

 

The Declarations

In the comments is the details of the off-the-shelf phone keypad I used which differs from that discussed in the Keypad tutorial.
My keypad has seven pins ( 1 to 7) and are connected as indicated to the Arduino pins 4 to 10.

For clarity above, the keypad pins are red and the Arduino pins are blue.

 

void setup(){
    Serial.begin(9600);
    keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

The setup( ) method

The keypad library can capture key pressed, key up and key hold events on each keypad key. Rather than directly process ke events through polling in teh loop( ) method, the keypad event listener is used, and is enabled in setup( ).

 

void loop(){
    char key = keypad.getKey();
}

The loop( ) method

The app needs to continuously exercise a call to get the current key pressed (if any) so that the keypad events can be captured by the listener. The loop( ) does not need to process the key char though.. 

 

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    bool ignore=false;
    switch (keypad.getState()){
    case PRESSED:
        Serial.print("+");
        break;
    case RELEASED:
          Serial.print("-");
        break;
    case HOLD:
        Serial.print("@");
        break;
    default:
    //Will be zero
        ignore=true;
        break;
    }
    if (!ignore)
      Serial.println(key);
}

The Event Handler (Listener)