#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";
bool passwordCheck = false;
enum State { DRIVE_DISABLED, PASSWORD_INPUT, DRIVE_ENABLED };
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 < 5000) {
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");
}
}
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;
} else {
passwordCheck = false; // Reset the password check
lcd.clear();
lcd.print("Incorrect");
delay(2000);
state = DRIVE_DISABLED; // Return to DRIVE_DISABLED state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
inputSequence = ""; // Clear the input sequence on an incorrect password
}
} else {
state = DRIVE_DISABLED; // Return to DRIVE_DISABLED state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Drive Disabled");
}
}
}
void printDigit(int button, char digit) {
if (digitalRead(button) == HIGH) {
lcd.setCursor(inputSequence.length(), 1);
lcd.print(digit);
inputSequence += digit;
// Wait for a short time to prevent multiple characters from being printed
delay(500);
while (digitalRead(button) == HIGH);
}
}