Key Input Library for Arduino

This page describes a simple library I wrote to handle connecting a set of pushbuttons to Arduino input pins for use as input keys.  You simply tell the library which pins are connected to pushbuttons and it then handles scanning and debouncing them.  The library should work with any model of Arduino board as it internally uses only calls to pinMode() and digitalRead() to work..  There are many ways to connect pushbutton switches to an Arduino.  If you have a large number of pushbuttons to connect, such as for a QUERTY keyboard, you would use a matrix of keys and code that would scans these by row and column.  The library described here is intended to use in simpler situations in which you only need to connect a handful of pushbuttons, as it requires the use one Arduino pin for each pushbutton.  This library is perfect, for example, for use with a 5-way, directional joystick switch, like the one:

To use the library, you first connect each pushbutton to an Arduino pin, such as the data pins 1 - 13, or the A0 - A5 analog pins.  Either will work fine.  Then, connect the other pin of each switch to ground, as shown in the following diagram:

The above diagram is intended to work with the example code included with the library, which is shown below and which will be included in the library (download using the link at the bottom of the page.)  The important thing to notice in the code is the C++ Constructor used to define the pins you want the library to use.  The constructor can take a variable number of parameters, but expects the first parameter to be a count of the number of parameters that follow.  Each parameter following the count parameter then defines the pin number for the connected key, such as 2, 3, A0, or A5.  In addition, it's important that you place the call to keyAvailable() in the Arduino loop() method where it will be called on a regular basis (ideally, at least 10 times a second) in order to not miss key presses.

When keyAvailable() returns true, you should then call getKeyIndex() to get the zero-based index value that indicates which button was pressed.  Index value 0 (zero), for example, means the button connected to the first pin defined in the constructor was the pin that as pressed.  If needed, you can call the method getKeyPin() and pass in the index value to get the number of the pin connected to that button.

#include <KeyLib2.h>

/*

 *  This code shows how to use the KeyLib2 libray to define a set of Arduino pins

 *  that have pushbuttons attached and connected to ground.  The libray then scans

 *  these pins, detects when one has been pressed (handles debouncing) and then

 *  returns an index value (0 though n-1) where n is the number of pins with buttons

 *  attached.

 *

 *  The first parameter passed to the KeyLib2() constructor is the number of pins

 *  with buttons attached followed by a list of pin numbers (0, 2, A0, etc.)

 *  Also, your code needs to periodically call keyAvailable() to initiate a scan

 *  of the pins and detect key pressed.  For efficiency keyAvailable() uses calls to

 *  millis() to limit key scans to 20 times/seconds.

 *  

 *  KeyLib2 under BSD BSD license (see KeyLib2.h)

 */

KeyLib2 pins(3, 2, 3, 4);

void setup () {

  Serial.begin(115200);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB

  }

  Serial.println("Ready");

}

void loop () {

  if (pins.keyAvailable()) {

    byte idx = pins.getKeyIndex();

    Serial.print("Pressed key index ");

    Serial.print(idx, DEC);

    Serial.print(" - connected to pin ");

    Serial.println(pins.getKeyPin(idx), DEC);

  }

}