#include <Wire.h> // библиотека для управления устройствами по I2C
#include <LiquidCrystal_I2C.h> // подключаем библиотеку для QAPASS 1602
#include <Keypad.h>
#include <EEPROM.h>
#define CORRECT_PASS_ADDR 0
#define CORRECT_PASS_INITED_FLAG_ADDR 255
#define DEFAULT_PASSWORD "1234"
const uint8_t KEYPAD_ROWS = 4;
const uint8_t KEYPAD_COLS = 3;
const char OK_KEY = '*';
const char RESET_KEY = '#';
const size_t MAX_PASSWORD_LENGTH = 16;
char keypadInputText[17];
char currentInputPosition = 0;
char correctPassword[] = "";
char keypadKeys[KEYPAD_ROWS][KEYPAD_COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ OK_KEY, '0', RESET_KEY }
};
uint8_t keypadColPins[KEYPAD_COLS] = { 5, 4, 3 }; // Pins connected to C1, C2, C3
uint8_t keypadRowPins[KEYPAD_ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keypadKeys), keypadRowPins, keypadColPins, KEYPAD_ROWS, KEYPAD_COLS);
LiquidCrystal_I2C LCD(0x27,16,2);
void setup() {
Serial.begin(9600);
prepareCorrectPass();
LCD.init();
LCD.backlight();
lcdClear();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY && currentInputPosition < MAX_PASSWORD_LENGTH) {
if (key == OK_KEY) {
handleInputComplete();
reset();
debugPrint("OK complete");
} else if (key == RESET_KEY) {
reset();
debugPrint("Reset complete!");
} else {
keypadInputText[currentInputPosition] = key;
currentInputPosition++;
lcdPrint(keypadInputText, 1);
}
}
delay(100);
}
void lcdPrint(char stringToWrite[], int lineNo) {
LCD.setCursor(0, lineNo);
LCD.print(stringToWrite);
}
void lcdClear() {
LCD.clear();
lcdPrint("Enter number:", 0);
}
void debugPrint(char stringToWrite[]) {
Serial.println(stringToWrite);
}
void debugPrint(int stringToWrite) {
Serial.println(stringToWrite);
}
void reset() {
currentInputPosition = 0;
memset(keypadInputText, '\0', sizeof(keypadInputText));
lcdClear();
}
void handleInputComplete() {
debugPrint("---------------");
debugPrint("Correct pass: ");
debugPrint(correctPassword);
debugPrint("Entered pass: ");
debugPrint(keypadInputText);
if (strcmp(correctPassword, keypadInputText) == 0) {
debugPrint("Match");
} else {
debugPrint("Not Match");
}
debugPrint("OK handleInput");
}
void prepareCorrectPass() {
bool isPasswordSet = false;
byte passwordSetFlagValue;
EEPROM.get(CORRECT_PASS_INITED_FLAG_ADDR, passwordSetFlagValue);
isPasswordSet = passwordSetFlagValue == 1;
if (!isPasswordSet) {
initPassword();
}
debugPrint("prepareCorrectPass");
debugPrint(passwordSetFlagValue);
debugPrint(DEFAULT_PASSWORD);
debugPrint(sizeof(DEFAULT_PASSWORD));
debugPrint("rom dump");
byte romByte;
for (size_t i = 0; i != 5; ++i) {
EEPROM.get(i, romByte);
debugPrint(romByte);
}
}
void initPassword() {
for (size_t i = 0; i != sizeof(DEFAULT_PASSWORD); ++i) {
EEPROM.put(CORRECT_PASS_ADDR + i, DEFAULT_PASSWORD[i]);
}
}