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

#define BUTTON 2
#define LCD_ADDRESS 0x27
#define LCD_ROW 2
#define LCD_COL 16
#define LED 8

LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_ROW, LCD_COL);
int incomingByte = 0;
int user = 0;
// ascii smum of register number 2347111
// 10 to account for the Serial buffered input having a trailing '\n'
const int sum = 355 + 10;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter Security Code");

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("PASSWORD");
}

void accept() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("B.K. 7111");
  lcd.setCursor(0, 1);
  lcd.print("66 75 8G491");
  delay(1000);
}

void reject() {
  digitalWrite(LED, HIGH);
  delay(1500);
  digitalWrite(LED, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:

  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    user += incomingByte;
  }
  if (user == sum) {
    accept();
  }
  if (digitalRead(BUTTON) == HIGH) {
    user = 0;
    digitalWrite(LED, LOW);
  } else {
    reject();
  }
}