#include <LiquidCrystal_I2C.h>

byte buttonPins[] = {13, 10, 6, 12, 9, 5, 11, 8, 4, 7};

// Make changes below:
char lock_button = 6; // Define the button used to lock the machine
byte passcode[] = {0, 1, 8, 6, 7, 4}; // Passcode from right to left = 8888888

#define relay 2 // Define the pin connected to the relay for locking/unlocking

// Make changes above:

int passcode_length, correct_cnt;
byte passcode_Temp[11];            // Max passcode length = 10
unsigned long sold_out_timer;
byte standby = true;

LiquidCrystal_I2C lcd(0x27, 16, 2);   // If necessary change the I2C address here

void setup() {
  lcd.init();
  lcd.backlight();

  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);

  for (int x = 0; x < 10; x++) {          // Change INPUT_PULLUP (switched logic) on all of the buttons
    pinMode(buttonPins[x], INPUT_PULLUP);
  }

  passcode_length = sizeof(passcode); // Calculate the length of the passcode

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   Welcome!    "); // Display the initial welcome message on the LCD
}

void loop() {
  int key = 20;

  for (int x = 0; x < 10; x++) {        // Check if any of the buttons are pressed, if yes change key variable with the corresponding key
    if (!digitalRead(buttonPins[x])) {
      key = x + 1;
      if (key == 10) key = 0;
    }
  }

  if (key != 20) {  // If a key is pressed

    for (int i = passcode_length; i > 0; i--) {
      // Shift the stored passcode to the right
      passcode_Temp[i] = passcode_Temp[i - 1];
    }

    passcode_Temp[0] = key; // Store the entered key in the passcode_Temp array
    standby = false; // Exit the standby mode
    sold_out_timer = millis(); // Reset the sold_out_timer (standby timer)
    lcd.setCursor(0, 0);
    lcd.print("Sold out.       ");  // Display the sold out message on the LCD
    correct_cnt = 0;

    // Check if the entered passcode matches the stored passcode
    for (int i = passcode_length - 1; i >= 0; i--) {
      if (passcode[i] == passcode_Temp[i]) correct_cnt++;
      else correct_cnt = 0;
    }

    // If the correct passcode is entered, unlock the machine
    if (correct_cnt == passcode_length) {
      bool unlocked = true;

      lcd.setCursor(0, 0);
      lcd.print("Welcome    "); // Display the "Unlocked" message on the LCD
      lcd.setCursor(0, 1);
      lcd.print("Mr. Harris");

      digitalWrite(relay, HIGH); // Activate the relay to unlock the machine

      delay(300); // Wait for a short duration to prevent double-click

      // Wait for the lock button to be pressed to lock the machine again
      while (unlocked) {
        if (!digitalRead(buttonPins[6 - 1])) unlocked = false;
      }
      delay(50);                // Debounce
      digitalWrite(relay, LOW); // Deactivate the relay to lock the machine
    }

    bool unpressed = false;

    while (!unpressed) {    // Wait till any of the buttons are not pressed
      int count = 0;
      for (int x = 0; x < 10; x++) {
        if (digitalRead(buttonPins[x])) {
          count++;
        }
      }
      if (count == 10) unpressed = true;
    }
    delay(60);      // Debounce buttons
  }

  // If in standby mode and sold out timer has elapsed, display the welcome message (stanby mode)
  if (!standby && sold_out_timer + 1000 < millis()) {
    standby = false;
    lcd.setCursor(0, 0);
    lcd.print("   Welcome!    "); // Display the welcome message on the LCD
    lcd.setCursor(0, 1);
    lcd.print("                ");
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
LEDs represents the locks