modifying keypad library

Greetings to all of you :smiley:

my problem is related to a specific library, possible to download from the arduino platform itself "Keypad by Mark Stanley, Alexander Breving Version 3.1.1."

the project im working on requires a keypad matrix that has 1 digit numbers/values stored/assignes to the keys 2 digit numbers/values stored/assignes to the keys and also 3 digit numbers/values stored/assigned to the keys.

with that library im unable to use 2 digits values ( like 10, 11, 12, 40, 60,...) and 3 digitvalues ( like 180, and so on).

as my skills in programing the code are more or less, less basic :smiley: im not able to come to a solution. therefore i turn myself to the community, and all others that might have an answer to my situation.

down below is the code i used to do a test.

is it possible to fix that with some datatype changes? or am i doomed?

#include <Keypad.h>

const byte ROWS1 = 1;
const byte COLS1 = 7; 
char keys1[ROWS1][COLS1] = {
  {'1','2','3','4','5','6','7'},
};
byte rowPins1[ROWS1] = {40}; //connect to the row pinouts of the keypad
byte colPins1[COLS1] = {41, 43, 45, 47, 49, 51, 53}; //connect to the column pinouts of the keypad

Keypad keypad1 = Keypad( makeKeymap(keys1), rowPins1, colPins1, ROWS1, COLS1 );

const byte ROWS2 = 1; 
const byte COLS2 = 7;
char keys2[ROWS2][COLS2] = {
  {'8','9','10','11','12','13'},

};
byte rowPins2[ROWS2] = {42}; //connect to the row pinouts of the keypad
byte colPins2[COLS2] = {41, 43, 45, 47, 49, 51, 53}; //connect to the column pinouts of the keypad

Keypad keypad2 = Keypad( makeKeymap(keys2), rowPins2, colPins2, ROWS2, COLS2 );

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char key1 = keypad2.getKey();
  
  if (key1){
    Serial.println(key1);
  }

    char key2 = keypad2.getKey();
  
  if (key2){
    Serial.println(key2);
  }
}

No need to change the library

Assign values such as 'A', 'B', 'C' and so on to the keys.

Declare an array containing the values that you want to get from the keypresses. Now, when you get a keypress, say 'A', subtract 65 from it (resulting in zero in the case of 'A') and use it as the index to the value that you want from the array.

You can use any 8-bit value other than zero. That gives you values from 1 to 255.

Do you really need two different keypads? Why not make them two rows of the same keypad? They already share all seven column pins.

#include <Keypad.h>

const byte ROWS1 = 1;
const byte COLS1 = 7;
char keys1[ROWS1][COLS1] =
{
  {1, 2, 3, 4, 5, 6, 7},
};
byte rowPins1[ROWS1] = {40}; //connect to the row pinouts of the keypad
byte colPins1[COLS1] = {41, 43, 45, 47, 49, 51, 53}; //connect to the column pinouts of the keypad

Keypad keypad1 = Keypad( makeKeymap(keys1), rowPins1, colPins1, ROWS1, COLS1 );

const byte ROWS2 = 1;
const byte COLS2 = 7;
char keys2[ROWS2][COLS2] =
{
  {8, 9, 10, 11, 12, 13},

};
byte rowPins2[ROWS2] = {42}; //connect to the row pinouts of the keypad
byte colPins2[COLS2] = {41, 43, 45, 47, 49, 51, 53}; //connect to the column pinouts of the keypad

Keypad keypad2 = Keypad( makeKeymap(keys2), rowPins2, colPins2, ROWS2, COLS2 );

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte key1 = keypad2.getKey();

  if (key1 != NO_KEY)
  {
    Serial.println(key1);
  }

  byte key2 = keypad2.getKey();

  if (key2 != NO_KEY)
  {
    Serial.println(key2);
  }
}

HI

i have already done that and put all of the numbers and letters into one keypad.

const byte ROWS1 = 7;
const byte COLS1 = 7;
char keys1[ROWS1][COLS1] = {
  {'1','2','3','4','5','6','7'},
  {'8','9','a','b','c','d','e'},
  {'f','g','h','i','j','k','l'},
  {'m','n','o','p','r','s','t'},
  {'u','v','x','y','z','A','B'},
  {'C','D','E','F','G','H','I'},
  {'J','K','L'},
};
byte rowPins1[ROWS1] = {40, 42, 44, 46, 48, 50, 52}; //connect to the row pinouts of the keypad
byte colPins1[COLS1] = {41, 43, 45, 47, 49, 51, 53}; //connect to the column pinouts of the keypad

Yet i struggle to do what UKHeliBob said to do. it sounds so simple.

Try this example of the technique

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
  {'0', '1', '2', '3'},
  {'4', '5', '6', '7'},
  {'8', '9', ':', ';'},
  {'<', '=', '>', '?'}
};

int translateKeys[] =
{
  123, 456, 789, 111,
  0, 1, 2, 3,
  22, 33, 44, 55,
  9, 10, 21, 52
};

byte colPins[COLS] = {2, 4, 7, 8}; //column pins  (R0 - R3)
byte rowPins[ROWS] = {10, 11, 12, A0}; //row pins  (L0 - L3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  char key = keypad.getKey();
  if (key)
  {
    Serial.print("Got ");
    Serial.print(key);
    Serial.print(" whose ASCII value is ");
    Serial.println((byte)key);
    Serial.print("subtracting 48 gives an array index of ");
    byte arrayIndex = byte(key) - 48;
    Serial.println(arrayIndex);
    Serial.print("The translated value in that position is ");
    Serial.println(translateKeys[arrayIndex]);
    Serial.println();
  }
}

The output is deliberately verbose to explain what is going on

Got 5 whose ASCII value is 53
subtracting 48 gives an array index of 5
The translated value in that position is 1

Got > whose ASCII value is 62
subtracting 48 gives an array index of 14
The translated value in that position is 21

HI again

I have to process that first to understand whats going on, and how.

thank you for you time and patience.

i will be back shortly, im sure of it.

cheers :smiley:

HI again

it has worked!!!!

thank you.

i hope i will be able to finish now my project, thanks to your fast responsiveness and great guide/ solution.

thank you!!!!

I'm glad it worked for you. Once you see the principle it is quite easy.

It might be easier to understand using numbers instead of characters as indexes:

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12},
  {13, 14, 15, 16}
};

int translateKeys[] =
{
  123, 456, 789, 111,
  0, 1, 2, 3,
  22, 33, 44, 55,
  9, 10, 21, 52
};

byte colPins[COLS] = {2, 4, 7, 8}; //column pins  (R0 - R3)
byte rowPins[ROWS] = {10, 11, 12, A0}; //row pins  (L0 - L3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  char key = keypad.getKey();
  if (key)
  {
    Serial.print("Got ");
    Serial.print(key);
    Serial.print("subtracting 1 gives an array index of ");
    byte arrayIndex = byte(key) - 1;
    Serial.println(arrayIndex);
    Serial.print("The translated value in that position is ");
    Serial.println(translateKeys[arrayIndex]);
    Serial.println();
  }
}