// STM32 Nucleo-C031C6 - Code Lock System with Keypad and LCD
#include "LiquidCrystal_I2C.h"
#include <Keypad.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Настройка клавиатуры 4x4
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {PA4, PA6, PB2, PB4};
byte colPins[COLS] = {PA0, PA1, PA7, PB5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Переменные кодового замка
String password = "1234";
String inputCode = "";
int wrongAttempts = 0;
bool systemLocked = false;
unsigned long lockTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("Code Lock System Starting...");
// Инициализация LCD
lcd.init();
lcd.backlight();
// Приветственное сообщение
lcd.setCursor(4, 0);
lcd.print("CODE LOCK");
lcd.setCursor(3, 1);
lcd.print("SYSTEM READY");
lcd.setCursor(5, 2);
lcd.print("WELCOME!");
delay(2000);
lcd.clear();
updateDisplay();
}
void updateDisplay() {
lcd.clear();
// Строка 0: Заголовок
lcd.setCursor(0, 0);
lcd.print("Enter 4-digit code:");
// Строка 1: Поле ввода со звездочками
lcd.setCursor(0, 1);
lcd.print("Code: ");
for(int i = 0; i < inputCode.length(); i++) {
lcd.print("*");
}
for(int i = inputCode.length(); i < 4; i++) {
lcd.print("_");
}
// Строка 2: Информация о попытках
lcd.setCursor(0, 2);
lcd.print("Attempts: ");
lcd.print(wrongAttempts);
lcd.print("/3");
// Строка 3: Подсказки
lcd.setCursor(0, 3);
lcd.print("#=OK *=Reset");
}
void loop() {
// Проверка блокировки
if(systemLocked) {
if(millis() - lockTime >= 10000) {
systemLocked = false;
wrongAttempts = 0;
inputCode = "";
updateDisplay();
}
return;
}
char key = keypad.getKey();
if(key) {
Serial.print("Key pressed: ");
Serial.println(key);
// Обработка цифр
if(key >= '0' && key <= '9') {
if(inputCode.length() < 4) {
inputCode += key;
updateDisplay();
}
}
// Обработка подтверждения (#)
if(key == '#') {
if(inputCode.length() == 4) {
if(inputCode == password) {
// Успешный вход
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("ACCESS GRANTED!");
lcd.setCursor(4, 1);
lcd.print("DOOR OPEN");
lcd.setCursor(0, 2);
lcd.print("Welcome back!");
lcd.setCursor(5, 3);
lcd.print(":-)");
Serial.println("Access Granted!");
wrongAttempts = 0;
delay(3000);
inputCode = "";
updateDisplay();
} else {
// Неудачная попытка
wrongAttempts++;
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("ACCESS DENIED!");
lcd.setCursor(3, 1);
lcd.print("WRONG CODE!");
lcd.setCursor(0, 2);
lcd.print("Attempts left: ");
lcd.print(3 - wrongAttempts);
Serial.print("Access Denied! Attempts: ");
Serial.println(wrongAttempts);
if(wrongAttempts >= 3) {
lcd.setCursor(1, 3);
lcd.print("SYSTEM LOCKED!");
systemLocked = true;
lockTime = millis();
delay(2000);
lcd.clear();
lcd.setCursor(3, 1);
lcd.print("LOCKED FOR");
lcd.setCursor(5, 2);
lcd.print("10 SECONDS");
} else {
delay(2000);
inputCode = "";
updateDisplay();
}
}
} else {
// Неполный код
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("Enter 4 digits!");
delay(1500);
updateDisplay();
}
}
// Обработка сброса (*)
if(key == '*') {
inputCode = "";
updateDisplay();
Serial.println("Input reset");
}
}
}