Keypad Library for Arduino Author: Mark Stanley, Alexander Brevig Contact: mstanley@technologist.com, alexanderbrevig@gmail.com
1.8 2009-07-08 - Alexander Brevig : Added no restrictions on sizes or keymaps
1.8 2009-07-08 - Alexander Brevig : Added no restrictions on sizes or keymaps
1.7 2009-06-18 - Alexander Brevig : Added setDebounceTime()
1.6 2009-06-13 - Mark Stanley : getKey() bug fixes
1.5 2009-05-19 - Alexander Brevig : Added setHoldTime()
1.4 2009-05-15 - Alexander Brevig : Added addEventListener()
1.3 2009-05-12 - Alexander Brevig : Added debouncing
1.2 2009-05-09: Changed getKey()
1.1 2009-03-12: Initial Release, NEW Library
1.0 2007: Initial Release

Keypad is a library for the Arduino.
This library is based upon the Keypad Tutorial code.
It is created to help Hardware Abstraction, and readability of code. It hides the pinMode, and digitalRead calls for the user.
Keypad library is part of the Hardware Abstraction libraries.
Download here: Keypad.zip
Put the Keypad folder in "hardware\libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->Keypad".
Once the library is imported, an "#inlcude <Keypad.h>" line will appear at the top of your Sketch.
Constructors:
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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
Instanciates a Keypad object that uses pins 5, 4, 3, 2 as row pins, and 8, 7, 6 as column pins.
This keypad has 4 rows and 3 columns, resulting in 12 keys.
Initialize all variables
The constructor does this for you.
Initializes the internal keymap to be equal to userKeymap
[See Keypad/Examples/CustomKeypad/CustomKeypad.pde for an example]
The overloaded constructor #3 does this for you.
Returns the key that is pressed, if any.
Returns the current state of the keypad.
The four states are IDLE, PRESSED, RELEASED and HOLD.
Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.
Set the amount of milliseconds the keypad will wait until it accepts a new keypress/keyEvent. This is the "time delay" debounce method.
Trigger an event if the keypad is used. You can load an example in the Arduino IDE
(File -> Sketchbook -> Examples -> Library-Keypad -> EventSerialKeypad) or see the KeypadEvent Example code.
How do I use multiple Keypads?
Keypad is a class. Therefore to use multiple Keypad, you must create an instance for each of them. In the example above, the Keypad instance keypad) was bound to the digital pins 2, 3, 4, 5, 6, 7 and 8.
To add a Keypad bound to digital pins 9, 10, 11, 12, 13, 14, 15 and 16, you could create the following instance keypad2:
const byte ROWS2 = 4; //four rows
const byte COLS2 = 3; //four columns
char keys2[ROWS2][COLS2] = {
{'.','a','d','1'},
{'g','j','m','2'},
{'p','t','w','3'},
{'*',' ','#','4'}
};
byte rowPins2[ROWS2] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins2[COLS2] = {16, 15, 14, 13}; //connect to the column pinouts of the keypad
Keypad keypad2 = Keypad( makeKeymap(keys2), rowPins2, colPins2, ROWS2, COLS2 );
And now it's just a matter of using whatever function is wanted on each keypad:
//update instances and possibly fire funcitons
void loop(){
char key1 = keypad.getKey();
char key2 = keypad2.getKey();
if (key1 != NO_KEY || key2 != NO_KEY){
Serial.print("You pressed: ");
Serial.print(key1 != NO_KEY ? key1 : "nothing on keypad");
Serial.print(" and ");
Serial.print(key2 != NO_KEY ? key2 : "nothing on keypad2");
Serial.println(".");
}
}
Can you show me an example of using setDebounceTime(unsigned int time)?
In Arduino follow the main menu from File-> Examples-> Keypad-> Examples-> DynamicKeypad. Once the sketch is open find setup() and there you will see:
void setup() {
Serial.begin(9600);
digitalWrite(ledPin, HIGH); // Turns the LED on.
keypad.addEventListener(keypadEvent); // Add an event listener.
keypad.setHoldTime(500); // Default is 1000mS
keypad.setDebounceTime(250); // Default is 50mS
}
This shows that the debounce time will allow one key press every 250 milliseconds. If multiple key presses occur within that time frame (as would happen when a key is bouncing) then those extra presses are simply ignored.
| Part of AlphaBeta Libraries. | |
| Last Modified: | March 04, 2010, at 08:03 PM |
| By: | mstanley |