#include <LiquidCrystal.h>

int Contrast = 60;
int button1 = 4;
int button2 = 3;
int button3 = 2;
int checkbutton = 5;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

String inputSequence = "";
String correctPassword = "123";
int attempts = 0;
unsigned long timeoutDuration = 5; // Initial timeout duration
unsigned long lastIncorrectTime = 0;
bool passwordCheck = false;

enum State { DRIVE_DISABLED, PASSWORD_INPUT, DRIVE_ENABLED, TIMEOUT };
State state = DRIVE_DISABLED;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(checkbutton, INPUT);
  analogWrite(8, Contrast);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Drive Disabled");
}

void loop() {
  if (state == DRIVE_DISABLED) {
    if (digitalRead(checkbutton) == HIGH) {
      state = PASSWORD_INPUT;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
      inputSequence = "";
      passwordCheck = false;
      delay(500);  // Debounce the check button
    }
  } else if (state == PASSWORD_INPUT) {
    passwordInput();
  } else if (state == DRIVE_ENABLED) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Drive Enabled");
    unsigned long startTime = millis();

    while (millis() - startTime < timeoutDuration * 1000) {
      if (digitalRead(checkbutton) == HIGH) {
        state = DRIVE_DISABLED;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Drive Disabled");
        return;  // Exit the loop and return to DRIVE_DISABLED state
      }
    }

    state = DRIVE_DISABLED;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Drive Disabled");
  } else if (state == TIMEOUT) {
    unsigned long currentTime = millis();
    unsigned long timeElapsed = (currentTime - lastIncorrectTime) / 1000;

    lcd.noCursor(); // Turn off the cursor temporarily
    lcd.setCursor(0, 0);
    lcd.print("Attempts: " + String(attempts));
    lcd.setCursor(0, 1);
    lcd.print("Timeout: ");
    lcd.print(timeoutDuration - timeElapsed);
    lcd.print("s       "); // Clear any residual characters
    lcd.cursor(); // Turn on the cursor

    if (timeElapsed >= timeoutDuration) {
      state = PASSWORD_INPUT;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    }
  }
}

void passwordInput() {
  printDigit(button1, '1');
  printDigit(button2, '2');
  printDigit(button3, '3');

  if (digitalRead(checkbutton) == HIGH) {
    if (!passwordCheck) {
      if (inputSequence == correctPassword) {
        lcd.clear();
        lcd.print("Correct Password");
        delay(2000);
        passwordCheck = true;
        state = DRIVE_ENABLED;
        attempts = 0;
        timeoutDuration = 5; // Reset the timeout duration to 5 seconds for the first attempt
      } else {
        passwordCheck = false;
        lcd.clear();
        lcd.print("Incorrect");
        delay(2000);
        inputSequence = "";
        passwordCheck = false;
        attempts++;

        if (attempts >= 2) {
          timeoutDuration = 10 + 5 * (attempts - 2); // Increment the timeout by 5 seconds for each attempt after the first
        }

        lastIncorrectTime = millis();
        state = TIMEOUT;
      }
    } else {
      state = DRIVE_DISABLED;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Drive Disabled");
      inputSequence = "";
      passwordCheck = false;
    }
  }
}

void printDigit(int button, char digit) {
  if (digitalRead(button) == HIGH) {
    lcd.setCursor(inputSequence.length(), 1);
    lcd.print(digit);
    inputSequence += digit;
    delay(500);

    while (digitalRead(button) == HIGH);
  }
}
$abcdeabcde151015202530354045505560fghijfghij