Creator: Andrew Mascolo Jr (HazardsMind), 3/14/2014

Special thanks to pYro_65, who helped me with the structure of the library.

Update:

Fixed deleteEventKey() function. Now it actually replaces the eventKey with an unused char, '~'

Description

This library allows the user to use a keypad of practically any size, with just one analog pin. It is based on a voltage divider design, like the one in this link, Analog Keypad.

A normal 4x3 or 4x4 keypad can be converted to use a single analog pin, like the Analog Keypad link above. Simply connect the Columns and Rows with two different resistors, ie. 1K and 4.7K.

  • Vref = +5V. (Please note, 3.3V is not stable enough to produce accurate analog readings)
  • R(a,b,c) = 4.7K.
  • R(d,e,f,g) = 1K.
  • Vout = analog pin of your choice.


This library is also capable of calculating the button values based on the resistor values. In addition, it can also interface with the Arduino Serial Monitor and/or a 16x2 Lcd (standard and I2C)


Getting started

To get started you need add some data into the constructor. This is how it will be displayed, either Serial or Lcd, the key map array, the number of rows and columns, the pin you want to use, and the resistor values.

This example here show how to set up the constructor.

#include <OnewireKeypad.h>

#define Rows 4
#define Cols 3
#define Pin A0
#define Col_Res 4700
#define Row_Res 1000

char KEYS[]= {
  '1','2','3',
  '4','5','6',
  '7','8','9',
  '*','0','#'
};

OnewireKeypad <Print, 12> KeyPad(Serial, KEYS, Rows, Cols, Pin, Col_Res, Row_Res );


<Print, 12> -> Print is associated with the hardwareSerial library, so you will need to have in the constructor, Serial. 12 indicates the number of buttons the keypad has.


The same can be done for using a Lcd.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OnewireKeypad.h>

#define Rows 4
#define Cols 3
#define Pin A0
#define Col_Res 4700
#define Row_Res 1000

char KEYS[]= {
  '1','2','3',
  '4','5','6',
  '7','8','9',
  '*','0','#'
};

LiquidCrystal_I2C Lcd(0x20,20,4); 
OnewireKeypad <LiquidCrystal_I2C, 12> KeyPad(Lcd,  KEYS, Rows, Cols, Pin, Col_Res, Row_Res )

With this example you can see it is 99% identical to that of the Serial constructor setup. However the difference here is, the object used when creating the Lcd display "Lcd" must also be used when creating the keypad.

Functions

OnewireKeypad()

Constructor.

Getkey()

Returns the key pressed.

SetHoldTime()

Sets how long until the key is considered Held down.

SetDebounceTime()

Sets the debounce time needed for debouncing the key

Key_State()

This shows if the key is not pressed(0), pressed(1), released(2) or held(3).

Readpin()

Returns the analog value of the key pressed.

LatchKey()

This latches the state of the key internally, but this does not effect the visible state of the key (wont repeat, " *key* Key is latched/not latched"). Hope that makes sense.

checkLatchedKey()

This returns true if the key pressed is latched.

addEventKey()

This will go to a function if the set key is pressed.

deleteEventKey()

This will delete the connection to the function and key.

ListenforEventKey()

This checks to see if the event key is ever pressed.


Download

Download Here: OneWireKeyPad.zip

This library MUST be put into the Arduino sketch folder "arduino\libraries\" To import this library, simply go to Sketch -> Import library ->OneWireKeyPad, in the side menu. If you do not see the library there, it is possible you put the library in the wrong spot or you did not extract the library. ** Double foldering will result in errors

Share