Keypad library questions

So I have a matrix keypad 8x8 I created a test to use the keypad library to read it and it works fine. However what I really need to do is set a bit when a key is pressed. I don't care or need to define a character. I simply want to turn a bit true when that key is pressed. I am actually just sending this value out the serial port. Can someone tell me if there is a way to define the keypad with a bit rather than a char? Basically I have defined boolean m[64] my 8x8 keypad just needs to set the corresponding bit high when and while its pressed. I can map everything to a charachter and then set the bit depending on the state of the keypad, but thought there might be a faster way with less conversion.

any insight would be great.


I’m not sure which keypad library you are using, or how your code is currently laid out to handle reading the state of the keys as you haven’t posted any… But you should likely be able to directly assign your digitalread keystate values directly to your boolean array. For example:

m[10]=digitalRead(pinTen);  //where pinTen=10;


for(a=0;a<63;a++) m[a]=digitalRead(a);

Attached is the Keypad section of Code. I am using the Keypad.h library. What I would like to do is just turn one of the m bits high while the key is pressed. I was wondering if I woudl have to go into the library and change it considerably to use Boolean values rather than characters. you will also notice the bytes I have set up, that is because I need to move all these bits back into bytes for sending out the comm port to another device.

//Setup Keypad
#include <Keypad.h>
const byte ROWS = 8; // Four rows
const byte COLS = 8; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 24, 23, 26, 25,28,27,30,29 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 32, 31, 34,33,36,35,38,37 }; 
boolean m[64]; //Set up Bits for Keys
byte n[16];     //Storage for Keys
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Hi bobert,

I took a quick look at the library which seems to support dynamic keypad fuctions. Check out the addEventListener(keypadEvent) function and the KeypadEvent Example code provided in the library documentation. The link to the example code is here:

Another resource to look at the is the DynamicKeypad.ino example program provided within the library.

You should be able to use these functions to update the values in your "m" array on a keypad press event. You can use Arduino's bitRead() and bitWrite() to populate your "n" array as required.

Last thing I wanted to point out is the boolean data type utilizes 1 full byte of memory so you may want to give some consideration to how rationalize your keypad data. You could potentially write the keystates directly to the "n" array using the bitWrite() function removing the need to temporarily store the keystate data in your "m" array.

Hope this makes sense and helps you out..


Thanks for the info, I actually started writing it all out using the M bits then switched and started using the bitWrite function to write directly to the N byte array. I also did utilize the KeypadEvent Function to speed up the whole thing. I was really wanting to remove the need to assign a character and if possible just assign the N bit directly. For now this works and is plenty fast for my needs. Here is the updated code...

//Runs when a keypad button is pressed
void keypadEvent(KeypadEvent key){

  switch (kpd.getState()){
  case PRESSED:
    switch (key){
      //First Row
    case 'y': bitWrite(n[0],0,1);break;
    case 'z': bitWrite(n[0],1,1);break;
    case '1': bitWrite(n[0],2,1);break;
    case '2': bitWrite(n[0],3,1);break;
    case '3': bitWrite(n[1],0,1);break;
    case '4': bitWrite(n[1],1,1);break;
    case '5': bitWrite(n[1],2,1);break;
    case '6': bitWrite(n[1],3,1);break;
    case 'U': bitWrite(n[2],0,1);break;
    case 'V': bitWrite(n[2],1,1);break;
    //Second Row
    case 'q': bitWrite(n[2],2,1);break;
    case 'r': bitWrite(n[2],3,1);break;
    case 's': bitWrite(n[3],0,1);break;
    case 't': bitWrite(n[3],1,1);break;
    case 'u': bitWrite(n[3],2,1);break;
    case 'v': bitWrite(n[3],3,1);break;
    case 'w': bitWrite(n[4],0,1);break;
    case 'x': bitWrite(n[4],1,1);break;
    case 'M': bitWrite(n[4],2,1);break;
    case 'N': bitWrite(n[4],3,1);break;
    //3rd Row
    case 'i': bitWrite(n[5],0,1);break;
    case 'j': bitWrite(n[5],1,1);break;
    case 'k': bitWrite(n[5],2,1);break;
    case 'l': bitWrite(n[5],3,1);break;
    case 'm': bitWrite(n[6],0,1);break;
    case 'n': bitWrite(n[6],1,1);break;
    case 'o': bitWrite(n[6],2,1);break;
    case 'p': bitWrite(n[6],3,1);break;
    case 'E': bitWrite(n[7],0,1);break;
    case 'F': bitWrite(n[7],1,1);break;
    //4th ROW
    case 'a': bitWrite(n[7],2,1);break;
    case 'b': bitWrite(n[7],3,1);break;
    case 'c': bitWrite(n[8],0,1);break;
    case 'd': bitWrite(n[8],1,1);break;
    case 'e': bitWrite(n[8],2,1);break;
    case 'f': bitWrite(n[8],3,1);break;
    case 'g': bitWrite(n[9],0,1);break;
    case 'h': bitWrite(n[9],1,1);break;
    case '7': bitWrite(n[9],2,1);break;
    case '8': bitWrite(n[9],3,1);break;
    //Function Keys
    case 'X': bitWrite(n[10],0,1);break;
    case 'Q': bitWrite(n[10],1,1);break;
    case 'O': bitWrite(n[10],2,1);break;
    case 'H': bitWrite(n[10],3,1);break;
    case 'A': bitWrite(n[11],0,1);break;
    case '9': bitWrite(n[11],1,1);break;
    case 'D': bitWrite(n[11],2,1);break;
    case 'C': bitWrite(n[11],3,1);break;
    case 'B': bitWrite(n[12],0,1);break;
    case 'L': bitWrite(n[12],1,1);break;
    case 'K': bitWrite(n[12],2,1);break;
    case 'J': bitWrite(n[12],3,1);break;
    case 'T': bitWrite(n[13],0,1);break;
    case 'S': bitWrite(n[13],1,1);break;
    case 'R': bitWrite(n[13],2,1);break;
    case '#': bitWrite(n[13],3,1);break;
    case '!': bitWrite(n[14],0,1);break;
    case 'Z': bitWrite(n[14],1,1);break;
    case '0': bitWrite(n[14],2,1);break;
    case 'G': bitWrite(n[14],3,1);break;
    case 'P': bitWrite(n[15],0,1);break;
    case 'I': bitWrite(n[15],1,1);break;
    case 'W': bitWrite(n[15],2,1);break;
    case 'Y': bitWrite(n[15],3,1);break;  

  case RELEASED:
    switch (key){
      //ROW 1
    case 'y': bitWrite(n[0],0,0);break;
    case 'z': bitWrite(n[0],1,0);break;
    case '1': bitWrite(n[0],2,0); break;
    case '2': bitWrite(n[0],3,0);break;
    case '3': bitWrite(n[1],0,0);break;
    case '4': bitWrite(n[1],1,0);break;
    case '5': bitWrite(n[1],2,0);break;
    case '6': bitWrite(n[1],3,0);break;
    case 'U': bitWrite(n[2],0,0);break;
    case 'V': bitWrite(n[2],1,0);break;
      //Row 2
    case 'q': bitWrite(n[2],2,0);break;
    case 'r': bitWrite(n[2],3,0);break;
    case 's': bitWrite(n[3],0,0);break;
    case 't': bitWrite(n[3],1,0);break;
    case 'u': bitWrite(n[3],2,0);break;
    case 'v': bitWrite(n[3],3,0);break;
    case 'w': bitWrite(n[4],0,0);break;
    case 'x': bitWrite(n[4],1,0);break;
    case 'M': bitWrite(n[4],2,0);break;
    case 'N': bitWrite(n[4],3,0);break;
    //row 3
    case 'i': bitWrite(n[5],2,0);break;
    case 'j': bitWrite(n[5],3,0);break;
    case 'k': bitWrite(n[5],0,0);break;
    case 'l': bitWrite(n[5],1,0);break;
    case 'm': bitWrite(n[6],2,0);break;
    case 'n': bitWrite(n[6],3,0);break;
    case 'o': bitWrite(n[6],0,0);break;
    case 'p': bitWrite(n[6],1,0);break;
    case 'E': bitWrite(n[7],2,0);break;
    case 'F': bitWrite(n[7],3,0);break;
    //Row 4
    case 'a': bitWrite(n[7],2,0);break;
    case 'b': bitWrite(n[7],3,0);break;
    case 'c': bitWrite(n[8],0,0);break;
    case 'd': bitWrite(n[8],1,0);break;
    case 'e': bitWrite(n[8],2,0);break;
    case 'f': bitWrite(n[8],3,0);break;
    case 'g': bitWrite(n[9],0,0);break;
    case 'h': bitWrite(n[9],1,0);break;
    case '7': bitWrite(n[9],2,0);break;
    case '8': bitWrite(n[9],3,0);break;
    //Function Keys
    case 'X': bitWrite(n[10],0,0);break;
    case 'Q': bitWrite(n[10],1,0);break;
    case 'O': bitWrite(n[10],2,0);break;
    case 'H': bitWrite(n[10],3,0);break;
    case 'A': bitWrite(n[11],0,0);break;
    case '9': bitWrite(n[11],1,0);break;
    case 'D': bitWrite(n[11],2,0);break;
    case 'C': bitWrite(n[11],3,0);break;
    case 'B': bitWrite(n[12],0,0);break;
    case 'L': bitWrite(n[12],1,0);break;
    case 'K': bitWrite(n[12],2,0);break;
    case 'J': bitWrite(n[12],3,0);break;
    case 'T': bitWrite(n[13],0,0);break;
    case 'S': bitWrite(n[13],1,0);break;
    case 'R': bitWrite(n[13],2,0);break;
    case '#': bitWrite(n[13],3,0);break;
    case '!': bitWrite(n[14],0,0);break;
    case 'Z': bitWrite(n[14],1,0);break;
    case '0': bitWrite(n[14],2,0);break;
    case 'G': bitWrite(n[14],3,0);break;
    case 'P': bitWrite(n[15],0,0);break;
    case 'I': bitWrite(n[15],1,0);break;
    case 'W': bitWrite(n[15],2,0);break;
    case 'Y': bitWrite(n[15],3,0);break;




That code is certainly is very verbose and cumbersome. You can try to automate things a little by trying something like this…

void keypadEvent(KeypadEvent key){
switch (kpd.getState())
  case PRESSED:
   byte match=0;
    for (byte y=0;y<ROWS;y++)
      if match==1 break; // key match found, exit y loop for efficiency
    for (byte x=0; x<COLS;x++)
      if key==keys[x][y] { bitWrite(n[y],x,1); match=1; break;}  // on key match to keys array update state variable and exit x loop for efficiency
  case RELEASED:
 // write similar routine for this case

Disclaimer - this code is untested and is provided only for example purposes. One thing I did note is that you’ve declared 16 bytes for storing keystate variable data in array ‘n’ when you probably only need 8 bytes for handling this task.