#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x3f,16,2); // set the LCD address to 0x3f for a 16 chars and 2 line display
// if you don´t know the I2C address of the display, use I2C scanner first (https://playground.arduino.cc/Main/I2cScanner/)
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
//===============================
//GLOBAL VARIABLES
//===============================
String permText = "Enter Code:";
String codeEntered = "";
const int finalCode = 1234;
const int code1 = 5555;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.backlight(); // enable backlight for the LCD module
//showSpalshScreen();
lcd.clear();
lcd.cursor();
codeEnterScreen();
}
void loop() {
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}
//========================================
//EVERYTHING OTHER THAN LOOP AND SETUP
//========================================
//char operation = 0;
//String memory = "";
String current = "";
//uint64_t currentDecimal;
//bool decimalPoint = false;
void showSpalshScreen() {
lcd.print("GoodArduinoCode");
lcd.setCursor(3, 1);
String message = "Calculator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(250);
}
void codeEnterScreen() {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(permText);
lcd.setCursor(5, 1);
lcd.print("****");
lcd.setCursor(5,1);
codeEntered = ""; //Clear Code
}
//===========================
//MAKE CURSOR FLASH
//===========================
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
//================================
//PROCESS KEY INPUT
//================================
void processInput(char key) {
if (key == '#') {
lcd.clear();
lcd.print(codeEntered);
lcd.noCursor();
delay(2000);
codeEnterScreen();
}
if ('A' != key && 'B' != key && 'C' != key && 'D' != key && '#' != key && '*' != key) {
codeEntered += String(key);
lcd.print(key);
Serial.println(codeEntered);
Serial.println(codeEntered.length());
if (codeEntered.length() >= 4) {
switch(codeEntered.toInt()){
case finalCode:
lcd.clear();
lcd.print("Code success");
lcd.noCursor();
delay(2000);
codeEnterScreen();
break;
case code1:
lcd.clear();
lcd.print("code 1");
lcd.noCursor();
delay(2000);
codeEnterScreen();
break;
default:
lcd.clear();
lcd.print("you suck!");
lcd.noCursor();
delay(2000);
codeEnterScreen();
break;
}
/*
if (finalCode == codeEntered.toInt()) {
lcd.clear();
lcd.print("You WIN");
lcd.noCursor();
delay(2000);
codeEnterScreen();
} else {
lcd.clear();
lcd.print("You Suck!");
lcd.noCursor();
delay(2000);
codeEnterScreen();
}
*/
}
}
//lcd.print(key);
}