question about the makeKeymap function in the keypad.h library

I saw a couple of programming tutorials about initialising a numeric keypad usually 4x3.
And I saw that when they use the makeKeymap function in

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

the 'keys' is a character array. Is it possible to use an int array instead? What about using ascii values because I need to make a '#' into an enter key. Essentially I am trying to make a simple calculator which can only do one type of function at a time.

Thanks

Here’s how I did my 16 button remote:

// set up the Keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

// Define the Keymap
char keys[ROWS][COLS] = 
{
  {    '1','2','3','A'      }  ,  // row 1  <<<< You could define these to be anything you want: for example '1','2','3','4'      
  {    '4','5','6','B'      }  ,  // row 2            '5','6','7','8'      
  {    '7','8','9','C'      }  ,  // row 3           '9','0','+','-'      
  {    '*','0','#','D'      }  ,  // row 4           '.','*','/','='      
  

// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins.
byte rowPins[ROWS] = { 6, 5, 4, 3  };  // Keypad uses internal pullups? No externals supplied
// these pins have separate external Diodes, anodes connected to Pin2 (Int0)
// Column pins are set low just before shut down,
// and Pin2 is pulled high internally.
// Pressing a Row button connects to a Low Columna and pulls the appropriate Row Diode Low for a Pin2 Low interrupt.
// Column pins are then set back high for the keypad library button determination.

// Connect keypad COL1, COL2, COL3, COL4 to these Arduino pins.
byte colPins[COLS] = { 10, 9, 8, 7 }; 

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

Thanks for the code that helped me understand the progamming better.

I would like to confirm some thing

// Define the Keymap
char keys[ROWS][COLS] = 
{
  {    '1','2','3','A'      }  ,  // row 1  <<<< You could define these to be anything you want: for example '1','2','3','4'      
  {    '4','5','6','B'      }  ,  // row 2            '5','6','7','8'      
  {    '7','8','9','C'      }  ,  // row 3           '9','0','+','-'      
  {    '*','0','#','D'      }  ,  // row 4           '.','*','/','='

Here you defined the keymap as a 2d array of characters charkeys[ROWS][COLS]

Is it possible to define the array as a 2d array of integers??

I suppose - may have to dig into the keypad library to change what is returned when a key is pressed.
Need a software guy for that.

anj17:
Is it possible to define the array as a 2d array of integers??

If you don’t mind using a C library you can just convert the ascii to integer on the fly.

#include <stdlib.h>

int key = atoi (kpd.getKey());

If you don’t mind using a C library you can just convert the ascii to integer on the fly.

Except that that code won’t work (or likely even compile). The atoi() function expects a NULL terminated array of chars, not one char.

I suppose - may have to dig into the keypad library to change what is returned when a key is pressed.

Why? Unless you want a single key to return a value other than the value on the face of a key, which would be very confusing. If the key.getKey() function returns a character, say ‘3’ when the 3 key is pressed, you can convert the ‘3’ to a 3 simply by subtracting ‘0’ from the character returned.

char keyLtr = key.getKey();
if(keyLtr >= '0' && keyLtr <= '9')
{
  int keyVal = keyLtr - '0';

  // Use the value ...
}

Hey PaulS
I do want to convert a '3' to a 3 and other numbers characters to integers, but my main concern are:

  1. Does arduino support all of the native c stuff?
  2. Can I use ascii values in the character array so that I can define the '#' key as an enter key?
  1. Does arduino support all of the native c stuff?

Almost all of it.

  1. Can I use ascii values in the character array so that I can define the '#' key as an enter key?

Like '\n' instead of '#' in the keymap? Of course.

You will still need code that detects that the key that returns '\n' was pressed, so, I can't see that returning '\n' instead of '#' accomplishes anything.

But, hey, it's your system.

Is it possible to define the array as a 2d array of integers??

Sure, here is a sketch I wrote yesterday to light a seven segment LED with a 4x3 keypad. (edit) It may be easier to do what you want if you code it directly without using a library. (edit)

// 7 segment LED display LSD3211-11 and 3X4 keypad
// pin 13 to segment a of display, 14 to b, 15 to c, etc.
// keypad plugged into Arduino using pins 0-6

int key, i, j, k;
//               a b c d e f g
int seg[][7] = {{1,1,1,1,1,1,0},  // 0 
                {0,1,1,0,0,0,0},  // 1 
                {1,1,0,1,1,0,1},  // 2 
                {1,1,1,1,0,0,1},  // 3 
                {0,1,1,0,0,1,1},  // 4 
                {1,0,1,1,0,1,1},  // 5 
                {1,0,1,1,1,1,1},  // 6 
                {1,1,1,0,0,0,0},  // 7 
                {1,1,1,1,1,1,1},  // 8 
                {1,1,1,1,0,1,1},  // 9
                {1,1,0,1,0,1,1},  // *, 10
                {0,1,1,0,1,1,0}}; // #, 11

int pad[][7] = {{-1, 2,-1, 0,-1, 8, 5},  // keypad 
                {-1,-1, 1,-1, 3,-1,-1},
                {-1,-1,-1,10,-1, 7, 4},
                {-1,-1,-1,-1,11,-1,-1},
                {-1,-1,-1,-1,-1, 9, 6}};
  
void setup() {
  for(i=13; i<20; i++)    // display uses digital pins 13-19
    pinMode(i, OUTPUT);
}

void loop() {
  for(j=0; j<5; j++) {                     // get one keypress
    pinMode(j, OUTPUT);                  
    digitalWrite(j, LOW);                  // set one pin LOW
    for(i=j+1; i<7; i++) {                 // check other pins
      pinMode(i, INPUT_PULLUP);            // which are HIGH
      if(digitalRead(i)==LOW) {            // if one found LOW
        key = pad[j][i];                   // save i, j values
        for(k=0; k<7; k++) {               
          digitalWrite(13+k, seg[key][k]); // show the keypress
        }        
      }
    }
    digitalWrite(j, HIGH);                 // set back to HIGH
  }                                 
}

The pad array contains integers so I could use them to directly index the seg array. The -1’s are just placeholders because some pin combinations don’t do anything.

PaulS:
Except that that code won’t work (or likely even compile). The atoi() function expects a NULL terminated array of chars, not one char.

Actually I tested that code before posting. Didn’t want to cause anyone any grief. :wink: But just to be sure I went back to the man-page for gcc and got this:

ATOI(3)                          Linux Programmer's Manual

NAME
       atoi, atol, atoll, atoq - convert a string to an integer

SYNOPSIS
       #include <stdlib.h>

       int atoi(const char *nptr);          <--- ????
       long atol(const char *nptr);
       long long atoll(const char *nptr);
       long long atoq(const char *nptr);

Is this non-portable between compilers?

But just to be sure I went back to the man-page for gcc and got this:

Which says that atoi() is expecting a pointer to some characters. The function knows when to stop processing the characters when it encounters a NULL. How you can have a character that needs to be converted to an int and a NULL, to terminate the processing, in the same memory location escapes me.

So today I tried to duplicate what I did with atoi() and like you said it failed to compile. Then I did the next thing that shouldn't work and that was:

char key = getKey();
int num = atoi(&key);

which doesn't eliminate the need for a null termination. However, it compiles and returns the correct integer for key. So the only thing I can think of is that the very next memory address after the character might (arbitrarily) contain a null. How else could it possibly work?

So the only thing I can think of is that the very next memory address after the character might (arbitrarily) contain a null.

The atoi() function stops when it encounters a NULL OR when it encounters a character that is not an integer character or plus or minus sign. So, the next memory address most likely contains something that is not '0' to '9', '+', or '-'.