Ardu? No!MTobjectsButton sets ≫ MTkeypad

MTkeypad, square matrix
(digital reading)

Event management for UNO, Nano, Mega

An MTkeypad object manages a square matrix of buttons.

Diagram for the simple square matrix

The buttons are on a grid of L rows and C columns, and we need L+C entries on the card. The matrix should be as square as possible (L and C equal to or different from 1). For example for 12 keys, a 4x3 matrix requires 7 pins, a 3x5 or 2x6 requires 8 pins and a 1x12 requires 13 pins.

6 bits7 bits8 bits9 bits10 bits
maxi9 buttons12 buttons16 buttons20 buttons25 buttons

The MTkeypad object can only read one key at a time. If multiple keys are pressed, it returns the key on the "lower row" and possibly the most "right", or even a key not pressed. This is due to the wiring itself. We can have up to 128 buttons (you need 15 pins!). It is not possible to use the keypad pins for other tasks because management is under interruption.

Advantages:
- interesting with 12 and 16 key keyboards already wired
- no additional components
Disadvantages:
- reading impossible if several keys are pressed
- other similar structures use fewer pins

Include to add

#include <MTobjects.h> // See http://arduino.dansetrad.fr/en/MTobjects

I strongly advise leaving the comment, if another person wanted to try the program, they would have a direct link download. This is valid for all the libraries you use.

Helpful comment

lines_on
columns_on

This comment is a word that can be added anywhere and which is ignored by the compiler.

Constructor

MTkeypad(PIN_LINES,
      PIN_COLUMNS,
      onSelectFunction = NO_ACTION,
      onUnselectFunction = NO_ACTION);

Useful functions

int8_t getKey(void);
virtual void onSelect(int8_t key);
virtual void onUnselect(void);

getKey: returns the number of the key pressed, -1 if none
onSelect: function to override called when the button is pressed
onUnselect: function to override called when the button is released

Bonus Features

void setOnSelectFunction(onSelectFunction);
void setOnSelectFunction();
void setOnUnselectFunction(onUnselectFunction);
void setOnUnselectFunction();

setOnSelectFunction: changes the function called when pressed
setOnSelectFunction: without parameters deletes the function called when pressed
setOnUnselectFunction: changes the function called on release
setOnUnselectFunction: without parameters removes the called function on release

Examples