4x4 matrix keypad with 2nd function

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.

Yes.

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/

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

#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");`
}
}

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

#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;
  }
}

Thanks so much, I am going through this now.