#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Настройка LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Адрес 0x27, дисплей 16x2

// Настройка клавиатуры
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Пины светодиодов
#define GREEN_LED 13
#define RED_LED A3

// База данных пользователей
struct User {
  String username;
  String password;
};

User users[] = {
  {"ABCD", "1234"},
  {"DAD", "5678"},
  {"BAD666", "666"},
  {"0000", "1111"},
  {"AC01", "AAAA"}
};

int userCount = sizeof(users) / sizeof(User);

String inputUsername = "";
String inputPassword = "";
bool isUsernameEntered = false;
int attempts = 0;

unsigned long blockTime = 0; // Время блокировки
bool isBlocked = false;

void setup() {
  Serial.begin(9600);
  lcd.init(); // Инициализация дисплея
  lcd.backlight(); // Включение подсветки
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter username:");
}

void loop() {
  if (isBlocked) {
    unsigned long currentTime = millis();
    if (currentTime - blockTime >= 300000) { // 5 минут блокировки
      isBlocked = false;
      attempts = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Unblocked!");
      delay(2000);
      resetSystem();
    }
    return;
  }

  char key = keypad.getKey();

  if (key) {
    if (!isUsernameEntered) {
      if (key == '#') {
        if (inputUsername.length() > 0) {
          isUsernameEntered = true;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Enter password:");
        }
      } else if (key == '*') {
        inputUsername = "";
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Enter username:");
      } else {
        inputUsername += key;
        lcd.setCursor(inputUsername.length() - 1, 1); // Вывод на второй строке
        lcd.print(key); // Отображаем сам символ
      }
    } else {
      if (key == '#') {
        bool isAuthenticated = false;
        for (int i = 0; i < userCount; i++) {
          if (users[i].username == inputUsername && users[i].password == inputPassword) {
            isAuthenticated = true;
            break;
          }
        }

        if (isAuthenticated) {
          digitalWrite(GREEN_LED, HIGH);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Access granted!");
          delay(2000);
          resetSystem();
        } else {
          attempts++;
          digitalWrite(RED_LED, HIGH);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Wrong password!");
          delay(1000);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Enter password:");

          if (attempts >= 3) {
            isBlocked = true;
            blockTime = millis();
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("System blocked!");
            delay(2000);
          }
        }

        inputPassword = "";
      } else if (key == '*') {
        inputPassword = "";
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Enter password:");
      } else {
        inputPassword += key;
        lcd.setCursor(inputPassword.length() - 1, 1); // Вывод на второй строке
        lcd.print(key); // Отображаем сам символ
      }
    }
  }
}

void resetSystem() {
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RED_LED, LOW);
  inputUsername = "";
  inputPassword = "";
  isUsernameEntered = false;
  attempts = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter username:");
}
$abcdeabcde151015202530fghijfghij