#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16},
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// abc1 def2 ghi3 <vol up>
// jkl4 mno5 pqr6 <vol down>
// stu7 vwx8 yz9. <caps lock>
// <backspace> <space> <enter> <esc>
// 97,98,99,49 100,101,102,50 103,104,105,51 v.up
// 106,107,108,52 109,110,111,53 112,113,114,54 v.dn
// 115,116,117,55 118,119,120,56 121,122,57,46 cps
// bckspc spc enter esc
const char keyCharArray[16][5] PROGMEM = {
"abc1", "def2", "ghi3", "1",
"jkl4", "mno5", "pqr6", "2",
"stu7", "vwx8", "yz9.", "3",
"7", "6", "5", "4",
};
char finalText[80] = "";
size_t finalTextIndex = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
char key = mygetKey();
}
char mygetKey() {
const unsigned long keyDelay = 500ul; //maximum delay between multiple presses of same key
static unsigned long keyTimer;
static size_t keyCharIndex = 0; //index of current character for multi-character key
static char keyPrevious = '\0'; //previous key pressed
static char keyPending = '\0'; //temporary storeage for key pressed within multi-charactere delay for another key
char key;
char keyChar = '\0'; //character to be returned from this function
if (keyPending == '\0') { //check for key pressed during multi-character delay for another key
key = keypad.getKey();
} else {
key = keyPending;
keyPending = '\0';
}
if (key) {
if (keyPrevious == '\0') {
if (strlen_P(keyCharArray[key - 1]) == 1) {
//key represents a single character
keyChar = (char)pgm_read_byte(&keyCharArray[key - 1][0]);
} else {
//key represents multiple characters
keyTimer = millis();
keyPrevious = key;
keyCharIndex = 0;
}
} else {
if (keyPrevious == key) { //same key has been pressed multiple times
if ((millis() - keyTimer) < keyDelay) {
keyCharIndex++; //advance to next character for key
if (keyCharIndex >= strlen_P(keyCharArray[key - 1])) {
keyCharIndex = 0;
}
keyTimer = millis();
}
} else { //key pressed during the timer delay for another key
keyChar = (char)pgm_read_byte(&keyCharArray[keyPrevious - 1][keyCharIndex]); //return character from previous key
keyPrevious = '\0';
keyPending = key; //save current key to be processed next time function is called
}
}
}
if ((keyPrevious != '\0') && ((millis() - keyTimer) > keyDelay)) { //return multi-character key press when timer runs out
keyChar = (byte)pgm_read_byte(&keyCharArray[keyPrevious - 1][keyCharIndex]);
keyPrevious = '\0';
}
if (keyChar == '1' || keyChar == '2' || keyChar == '3' || keyChar == '4' || keyChar == '5' || keyChar == '6' || keyChar == '7') {
Serial.print(F("fuck key"));
}
else
{
Serial.print(keyChar);
return keyChar;
}
}