A matrix keypad is the kind of keypad you see on microwave ovens, gas pumps, and calculators.  A matrix keypad you can connect to a breadboard is also great for prototypes and inventions where things like codes, times, or other values have to be entered.

## Circuit

Note: Be careful when making your 4x4 Keypad connections. The FLiP and ABWX diagrams below use the same pins, but the orientation of your circuit and keypad differs depending on which board you use.

## Test Code

The test program updates the Terminal showing whichever single key is pressed, every 1.5 s.  If you're not pressing a key when it checks, the program will display -1.  So, for best results, press and hold the key you want to try.  Keep in mind, this is just test code.  The Try This and Your Turn examples will demonstrate how to make it responsive as you type in numbers, how to do simple mathematic functions, and more.

Now, you're ready to try the first example program

•  Click SimpleIDE's Open Project button, and open Keypad 4x4 Read Keys.side from ...Documents/SimpleIDE/Learn/Examples/Devices/Interface/.
• Make sure your board has power, and the COM port is correctly set.
• Use SimpleIDE's Run with Terminal button.
• When the SimpleIDE terminal appears and starts displaying "Key = -1", try pressing and holding various buttons and verify that the program detects and displays the button you press.

## How it Works

This 4x4 matrix keypad has 16 built-in pushbutton contacts connected to row and column lines.  A microcontroller can scan these lines for a button-pressed state.  In the keypad library, the Propeller sets all the column lines to input, and all the row lines to input.  Then, it picks a row and sets it high.  After that, it checks the column lines one at a time.  If the column connection stays low, the button on the row has not been pressed.  If it goes high, the microcontroller knows which row (the one it set high), and which column, (the one that was detected high when checked). See the schematic in the "Circuit" section, above, for a visual reference of the keypad layout.

The keypad library supports pretty much any number of rows and columns.  So, the program has to tell it our keypad is has 4 rows and 4 columns, which I/O pins the lines are connected to, and what value each button represents.  The rows, cols, and values arrays store that information.  The rows array will be used to tell the keypad library that the top row is connected to P7, the second row to P6 and so on.  Likewise, the cols array lists the leftmost column as connected to P3, the next over connected to P2 and so on.  The values array stores the value we want the program to give us for each button press.  For example, if the top-left button is pressed, we want the number 1, and if the next one over is pressed, we want the number two.  If the top right button is pressed, we want the ASCII code for the 'A' character, which is 65.

Inside the main function, keypad_setup gets the number of rows (4), the number of columns (also 4), the rows array, the cols array, and the values array.  After that, key = keypad_read() will return -1 if no buttons are pressed.  If a button is pressed, it will return the value from the array that corresponds to that button.  For example, if you press a button on the 3rd row, second column, the keypad_read function will return the number 8, which will get stored in the key variable.  To display it correctly, an if statement checks for values less than or equal to 9, and displays them with %d, decimal-integer formatting flag .  The ASCII codes for '*', '#', 'A', 'B', 'C', and 'D' are 35, 42, 65, 66, 67, and 68, all of which are above 9, and get displayed with the print statement that uses the %c character formatting flag.

```/*
*/

#include "simpletools.h"                      // Libraries simpletools & keypad

int rows[4] = {7, 6, 5, 4};                   // Row I/O pins (top to bottom)
int cols[4] = {3, 2, 1, 0};                   // Column I/O pins (left to right)

int values[16] = {  1,   2,    3,  'A',       // Values for each key in the 4x4
4,   5,    6,  'B',
7,   8,    9,  'C',
'*',   0,  '#',  'D' };

int key;                                      // Variable stores keypad key

int main()                                    // Main function
{

while(1)                                    // Main loop
{

if(key <= 9)                              // Display key value
print("key = %d\r", key);               // If <= 9, dispaly as decimal
else
print("key = %c\r", key);               // Otherwise, display as character
pause(1500);                              // Wait 1.5 s before repeat
}
}```

## Did You Know?

If you want the Propeller to rememeber a value 123 when after you press the 1, 2, and 3 buttons, here's how it works:

1. When the 1 is pressed add it to a variable (we'll use the number variable).
2. Wait for the 1 button to be released.
3. When 2 is pressed, muliply number by 10, then add 2.
4. Wait for the 2 button to be released.
5. When 3 is pressed, muliply number (which holds 12) by 10, and add 3.
6. Wait for the 3 button to be released.
7. If the button is pressed that signals you're done typing digits, save or display the value.

## Try This

This example program allows you to press/release digits to build numbers.  For example, if you press and release 1, then 2, then 3, then #, the program will display "You entered 123".  Keep in mind that it's not just rememebering the digits 1, 2, and 3, it's creating the number 123 by multiplying what's already in the number variable by 10, and then adding the most recently pressed digit.  Once the Propeller has this value, it could be used as a count-down timer in an oven, or the first value used in a calculator operation.

```/*
Demonstrates how to build numbers with multiple key presses.
*/

#include "simpletools.h"                      // Libraries simpletools & keypad

int rows[4] = {7, 6, 5, 4};                   // Row I/O pins (top to bottom)
int cols[4] = {3, 2, 1, 0};                   // Column I/O pins (left to right)

int values[16] = {  1,   2,    3,  'A',       // Values for each key in the 4x4
4,   5,    6,  'B',
7,   8,    9,  'C',
'*',   0,  '#',  'D' };

int number = 0;                               // Stores number result

int main()                                    // Main function
{

print("Type a number, then press #\r");     // User prompt

while(1)                                    // Main loop
{

print("\r");                              // Next line
print("You entered %d\r", number);        // Display result
}
}

```