#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
int R1 = 1;
int C1 = 1;
int count;
byte pin_rows[ROW_NUM] = {A15, A14, A13, A12}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {A11, A10, A9, A8}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// #define Single_long_click_duration 10000
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
int dataNumber = 0; // new for this version
char Export[4];
byte a = 0;
unsigned long startTime;
bool holding = false;
bool longPress = false;
bool repeating = false;
int keyHeld;
String msg;
unsigned long startHold = 0;
uint16_t intervalLongPress = 1000;
unsigned long startRepeat = 0;
uint16_t intervalRepeat = 250;
void setup() {
Serial.begin(115200);
lcd.init(); // display initialization
lcd.clear(); // clear the screen fully
lcd.backlight(); // activate the backlight
// Export[0] = 0x0; // Set "Export" buffer string to an empty string
}
void loop()
{
lcd.setCursor(C1, R1);
char key = checkKeys();
if (key)
{
switch (key)
{
case '0'...'9':
if (!longPress && !repeating) {
if (C1 < 4)
{
Export[a++] = key;
Export[a] = 0x0;
if (atoi(Export) > 444)
{
Export[--a] = 0x0;
break;
}
lcd.print(key);
C1++;
// count = atoi(Export);
}
}
break;
case 'B':
if (!longPress) {
if (C1 > 1)
{
C1--;
if (C1 < 1)
C1 = 1;
Export[--a] = 0x0;
count = atoi(Export);
lcd.setCursor(C1, R1);
lcd.print(' ');
}
} else {
//all clear
lcd.setCursor(11, 1);
lcd.print(" ");
count = 0;
memset(Export, 0, sizeof(Export));
a = 0;
C1 = 1;
lcd.setCursor(C1, R1);
lcd.print(" ");
}
break;
case '#':
if (!longPress) {
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(Export);
count = atoi(Export); //Converting 'chart to int' VIP
a = 0;
C1 = 1;
lcd.setCursor(C1, R1);
lcd.print(" ");
}
break;
case 'C':
if (!longPress) {
if (count < 444)
{
count = count + 1;
}
else
{
count = 1;
}
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(count);
}
break;
case 'D':
if (!longPress) {
if (count > 1)
{
count = count - 1;
}
else
{
count = 444;
}
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(count);
}
break;
} //end switch key
//crop the long press
longPress = false;
} // end if key
}
char checkKeys() {
char result = 0;
repeating = false;
if (kpd.getKeys())
{
for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
{
if ( kpd.key[i].stateChanged ) // Only find keys that have changed state.
{
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
case PRESSED:
msg = " PRESSED. ";
break;
case HOLD:
msg = " HOLD. ";
startHold = millis();
holding = true;
keyHeld = i;
break;
case RELEASED:
msg = " RELEASED.";
holding = false;
result = kpd.key[i].kchar;
break;
case IDLE:
msg = " IDLE. ";
break;
}
Serial.print("Key ");
Serial.print(kpd.key[i].kchar);
Serial.println(msg);
// lcd.setCursor(0, 1);
// lcd.print(kpd.key[i].kchar);
// lcd.print(msg);
} // End if ( kpd.key[i].stateChanged )
} // End for (int i=0; i<LIST_MAX; i++)
} // End kpd.getKeys()
if (holding) {
if (millis() - startHold >= intervalLongPress)
{ //holding = false;
result = kpd.key[keyHeld].kchar;
startHold = millis();
msg = " LONG PRESS. ";
Serial.print("Key ");
Serial.print(kpd.key[keyHeld].kchar);
Serial.println(msg);
longPress = true;
// lcd.setCursor(0, 1);
// lcd.print(kpd.key[keyHeld].kchar);
// lcd.print(msg);
} else if (millis() - startRepeat >= intervalRepeat) {
startRepeat = millis();
result = kpd.key[keyHeld].kchar;
repeating = true;
Serial.print("Repeating Key ");
Serial.print(kpd.key[keyHeld].kchar);
}
}
return result;
}