#include <Keypad.h>
const uint8_t ROWS = 3;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', 'U', 'V'},
{ '4', 'X', '2'},
{ '3', 'L', 'D'}
};
uint8_t colPins[COLS] = { 9, 4, 6}; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = { 5, 7, 8}; // Pins connected to R1, R2, R3
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char holdkey;
unsigned long t_hold;
unsigned long start_time = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
//single simple key press
if (key) {
holdkey = key;
//Serial.println(keypad.getState( ));
Serial.print("key pressed: ");
Serial.println(key);
}
//continuos execution of hold
if (keypad.getState() == HOLD) {
//Serial.print("yes hold ");
if ((millis() - t_hold) > 500 ) { //time how long for a refresh
switch (holdkey) {
case 'U':
Serial.print("Key hold: ");
Serial.println(holdkey);
break;
case 'V':
Serial.println("Move Up");
break;
case 'L':
Serial.println("Move Right");
}
t_hold = millis();
}
}
}