#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include<Keypad.h>
// char customKey=0;
#include <TM1637Display.h>
#define CLK 3
#define DIO 2
TM1637Display display = TM1637Display(CLK, DIO);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A15, A14, A13, A12}; //row pinouts of the keypad
byte colPins[COLS] = {A11, A10, A9, A8}; //column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
int LCDCol = 1;
int LCDRow = 1;
char digits[4];
byte d = 0;
int count = 0;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(LCDCol, LCDRow);
// Set "digit" buffer string to an empty string
digits[0] = 0x0;
}
void loop()
{
display.setBrightness(4);
char key = customKeypad.getKey();
if (key)
{
lcd.setCursor(LCDCol,LCDRow);
switch (key)
{
case '0' ... '9': // Numbers
if(d < 3)
{
// Store the new digit as the last one
digits[d++] = key;
// End of the string
digits[d] = 0x0;
// Print the new "digits" string
showDigits();
} else {
// No more than 3 digits! Ignore exceeding one!
Serial.print("Ignored key '");
Serial.print(key);
Serial.println("'");
}
break;
case 'B': // Backspace (delete last)
if(LCDCol>1)
{
LCDCol--; // Decrease column
// But not less than column 1
if(LCDCol<1)
LCDCol=1;
// End of the string at 1 character less than before
digits[--d] = 0x0;
lcd.setCursor(LCDCol,LCDRow);
lcd.print(' ');
Serial.println("Backspace!");
}
break;
case '#': // Display current typed number
showDisplay();
break;
case 'C': // Increment counter
++count;
updateDigits();
showDisplay();
break;
case 'D': // Decrement counter
--count;
updateDigits();
showDisplay();
break;
case '*': // CLEAR ALL
updateDigits();
// showDisplay();
Serial.println("Clear!");// Uncomment this if the clear should reset the digits at col 11 also
break;
default:
Serial.print("Unmapped key '");
Serial.print(key);
Serial.println("'");
}
} // end of key
} // end of void loop
void showDisplay()
{ // Read the "digits"and show the value on the external display
lcd.setCursor(11,1);
lcd.print(" ");
lcd.setCursor(11,1);
lcd.print(digits);
// Read "digits" string and convert it into integer value
count = atoi(digits);
display.showNumberDec(count);
// Resets local LCD "digits"
resetDigits();
}
void resetDigits()
{ // Resets the locally typed digits (LCD)
lcd.setCursor(1,1);
lcd.print(" ");
LCDCol = 1;
d = 0;
// digits[0] = 0x0;
// showDigits();
}
void showDigits()
{ // Print on LCD the locally typed digits
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(digits);
}
void updateDigits()
{ // Convert back "count" value into "digits" string
itoa(count, digits, 10);
lcd.setCursor(1,1);
lcd.print(" ");
LCDCol = 1;
d = 0;
}