# 4x4 matrix keypad with 2nd function

**URL:** <https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766>\
**Category:** Programming\
**Created:** [January 26, 2022, 6:58pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766 "2022-01-26T18:58:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alarape](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alarape](https://forum.arduino.cc/u/Alarape)\
**Post date:** [January 26, 2022, 6:58pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/1 "2022-01-26T18:58:35Z")

</div>

Hi,

any possibility to display all numeric keys on the serial monitor while other keys as function keys on during long tap?

kindly help.

thanks.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 26, 2022, 8:48pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/2 "2022-01-26T20:48:15Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [January 26, 2022, 9:33pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/3 "2022-01-26T21:33:28Z")

</div>

> [@Alarape](#):
>
> any possibility to display all numeric keys on the serial monitor while other keys as function keys on during long tap?

I think you are asking if keypad keys could be used for their normal keys for presses less than some time threshold or function keys if presses are beyond the time threshold.

The answer is yes. If you are using the keypad library just monitor the **getKey()** function every time through loop. Use the **millis()** function to determine if the time the key has been pressed exceeds the threshold.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [January 26, 2022, 10:40pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/4 "2022-01-26T22:40:48Z")

</div>

> [@ToddL1962](#):
>
> I think you are asking if keypad keys could be used for their normal keys for presses less than some time threshold or function keys if presses are beyond the time threshold.
> 
> The answer is yes. If you are using the keypad library just monitor the **getKey()** function every time through loop. Use the **millis()** function to determine if the time the key has been pressed exceeds the threshold.

Actually now that I look at the library you can use the key state functions and the hold time setting to accomplish what you want.

[https://playground.arduino.cc/Code/Keypad/](https://playground.arduino.cc/Code/Keypad/)

---

<div class="post-metadata">

**Author:** ![Alarape](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alarape](https://forum.arduino.cc/u/Alarape)\
**Post date:** [January 27, 2022, 12:59pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/5 "2022-01-27T12:59:52Z")

</div>

Thanks for the link, it very helpful, but still trying to figure out the use of HOLD function as control statement.

---

<div class="post-metadata">

**Author:** ![Alarape](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alarape](https://forum.arduino.cc/u/Alarape)\
**Post date:** [January 27, 2022, 1:04pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/6 "2022-01-27T13:04:24Z")

</div>

#include \<Keypad.h\>  
#include \<ctype.h\>  
const byte ROWS = 4;  
const byte COLS = 4;

char numberKeys[ROWS][COLS] = {  
{'1', '2', '3', 'A'},  
{'4', '5', '6', 'B'},  
{'7', '8', '9', 'C'},  
{'\*', '0', '#', 'D'}  
};

const byte rowPins[ROWS] = {9, 8, 7, 6};  
const byte colPins[COLS] = {5, 4, 3, 2};  
unsigned long startTime;  
Keypad numKeypad = Keypad(makeKeymap(numberKeys),rowPins, colPins, ROWS, COLS);

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

```
         }

```

void loop(){  
char numKey = numKeypad.getKey();

if ((numKey=='1')||(numKey=='2')||(numKey=='3')||(numKey=='4')||(numKey=='5')||(numKey=='6')||(numKey=='7')||(numKey=='8')||(numKey=='9')||(numKey=='0')){  
Serial.println(numKey);  
}  
if (numKey =='A'){  
Serial.println("start filling");  
}  
if (numKey =='B'){  
Serial.println("stop filling");  
}  
if (numKey =='C'){  
Serial.println("tare");  
}  
if (numKey =='D'){  
Serial.println("enter");  
}  
if (numKey =='#'){  
Serial.println("select");  
}  
if (numKey =='\*'){  
Serial.println("reset");`  
}  
}

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [January 27, 2022, 4:28pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/7 "2022-01-27T16:28:22Z")

</div>

> [@Alarape](#):
>
> Thanks for the link, it very helpful, but still trying to figure out the use of HOLD function as control statement.

I coded this using the key state functions. All you have to add is key processing and adjust the hold time.

```arduino
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A3,A2,A1,A0}; //connect to the column pinouts of the keypad

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

const unsigned int funcKeyMillis = 2000;
bool isFuncKey = false;

void setup()
{
  Serial.begin(9600);
  myKeypad.setHoldTime(funcKeyMillis);
  myKeypad.setDebounceTime(50);
}

void loop()
{
  static char key;
  static KeyState lastKeyState = IDLE;
  
  char currKey = myKeypad.getKey();
  KeyState currKeyState = myKeypad.getState();
    
  if (currKeyState != lastKeyState)
  {
    Serial.print("State Change = ");
    switch (currKeyState)
    {
      case IDLE:
        Serial.println("IDLE");
        break;

      case PRESSED:
        Serial.println("PRESSED");
       key = currKey;
        Serial.print("Key Pressed = ");
        Serial.println(key);
        isFuncKey = false;
        break;

      case RELEASED:
        Serial.println("RELEASED");
        Serial.print("Process ");
        Serial.print(key);
        Serial.print(" as a ");
        if (isFuncKey)
        {
          // Process as a function key
          Serial.println("function key");
        }
        else
        {
          // Process as a normal key
          Serial.println("normal key");
        }
        break;

      case HOLD:
        Serial.println("HOLD");
        isFuncKey = true;
        break;
    }
    lastKeyState = currKeyState;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Alarape](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alarape](https://forum.arduino.cc/u/Alarape)\
**Post date:** [January 27, 2022, 5:02pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/8 "2022-01-27T17:02:38Z")

</div>

**Thanks so much, I am going through this now.**

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 26, 2022, 5:03pm UTC](https://forum.arduino.cc/t/4x4-matrix-keypad-with-2nd-function/951766/9 "2022-07-26T17:03:05Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
