Arduino Nano Door Lock Code by Techno Dictions

#include 
#include 
#include 
#include 
#include 

LiquidCrystal_I2C lcd(0x27,16,2);

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[4] = {2,3,4,5};
byte colPins[4] = {A3,A2,A1,A0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

#define CLK 7
#define DIO 8
TM1637Display display(CLK, DIO);

#define BUZZER_PIN 6
#define SERVO_PIN 9
#define LED_PIN 12

Servo lockServo;

String password = "786C";
String entered = "";
int wrongAttempts = 0;

unsigned long lastActivityTime = 0;
const unsigned long timeout = 15000;
bool screenOff = false;
bool lockedOut = false;
bool overridePressed = false;

void beep(int duration=100) {
  digitalWrite(BUZZER_PIN, HIGH);
  delay(duration);
  digitalWrite(BUZZER_PIN, LOW);
}

void showErrOnDisplay() {
  uint8_t ERR[] = {0x79, 0x50, 0x50, 0x00};
  display.setSegments(ERR);
}

void showOpenOnDisplay() {
  uint8_t OPEN_txt[] = {0x3F, 0x73, 0x79, 0x54};
  display.setSegments(OPEN_txt);
}

void showIdleOnDisplay() {
  uint8_t dash[] = {0x40, 0x40, 0x40, 0x40};
  display.setSegments(dash);
}

void lockoutAlarm() {
  lockedOut = true;
  lcd.clear();
  lcd.backlight();
  lcd.print("System Locked");
  showIdleOnDisplay();
  digitalWrite(LED_PIN, HIGH);

  unsigned long lockStart = millis();

  // Buzzer for first 10 sec
  while (millis() - lockStart < 5000) {
    digitalWrite(BUZZER_PIN, HIGH);
    // also check for override
    char key = keypad.getKey();
    if(key == '*' || key == 'B') {
      overridePressed = true;
    }
    if(overridePressed) break;
  }
  digitalWrite(BUZZER_PIN, LOW);

  // Keep locked for total 60 sec
  while ((millis() - lockStart < 60000) && !overridePressed) {
    char key = keypad.getKey();
    if(key == '*') {
      char nextKey = 0;
      while(!nextKey) nextKey = keypad.getKey();
      if(nextKey == 'B') {
        overridePressed = true;
      }
    }
  }

  digitalWrite(LED_PIN, LOW);
  lockedOut = false;
  wrongAttempts = 0;
  lcd.clear();
  lcd.print("Enter PIN:");
  showIdleOnDisplay();
}

void setup() {
  lcd.init();
  lcd.backlight();
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  lockServo.attach(SERVO_PIN);
  lockServo.write(0);
  display.setBrightness(5);
  showIdleOnDisplay();

  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Welcome to door");
       lcd.setCursor(2,3);
    lcd.print("Lock System");

  

  delay(3000);
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("A project by");
  lcd.setCursor(3,1);
  lcd.print("TECHNO DICTIONS");
  delay(4000);
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("Click A to");
  lcd.setCursor(4,1);
  lcd.print("continue");

  while(true) {
    char key = keypad.getKey();
    if(key == 'A') {
      beep();
      lcd.clear();
      lcd.print("Enter PIN:");
      lastActivityTime = millis();
      screenOff = false;
      break;
    }
  }
}

void loop() {
  if(lockedOut) {
    lockoutAlarm(); // stays inside until unlocked
    return;
  }

  char key = keypad.getKey();
  if(!screenOff && millis() - lastActivityTime > timeout) {
    lcd.noBacklight();
    lcd.clear();
    screenOff = true;
    showIdleOnDisplay();
  }

  if(key) {
    beep();
    lastActivityTime = millis();

    if(key == 'A') {
      lcd.backlight();
      if(screenOff) {
        lcd.clear();
        lcd.print("Enter PIN:");
        entered = "";
        lcd.setCursor(0,1);
        screenOff = false;
        showIdleOnDisplay();
      }
    }
    else if(key == '#') {
      if(entered == password) {
        lcd.clear();
        lcd.print("Unlocked!");
        showOpenOnDisplay();
        lockServo.write(90);
        beep(300);
        delay(2000);
        lockServo.write(0);
        delay(3000);

        lcd.clear();
        lcd.noBacklight();
        screenOff = true;
        wrongAttempts = 0;
        showIdleOnDisplay();
      } else {
        lcd.clear();
        lcd.print("Wrong!");
        showErrOnDisplay();
        for(int i=0; i<3; i++) {
          digitalWrite(LED_PIN, HIGH);
          beep(100);
          delay(150);
          digitalWrite(LED_PIN, LOW);
          delay(150);
        }
        wrongAttempts++;
        if(wrongAttempts >= 5) {
          overridePressed = false;
          lockoutAlarm();
        } else {
          lcd.clear();
          lcd.print("Enter PIN:");
        }
      }
      entered = "";
      lcd.setCursor(0,1);
    }
    else if(key == 'D') {
      if(entered.length() > 0) {
        entered.remove(entered.length()-1);
        lcd.clear();
        lcd.print("Enter PIN:");
        lcd.setCursor(0,1);
        lcd.print(entered);
      }
    }
    else {
      entered += key;
      lcd.print(key);
    }
  }
}