#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ledPin = 8;
char fullName[] = "Visesh Agarwal";
const double registerNumber = 2347164;
// Initialize variables for tracking unauthorized attempts
int attempts = 0;
bool unauthorizedAccess = false;
void setup() {
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Start serial communication
Serial.println("System Initialized.");
Serial.println("Please enter your code followed by ENTER.");
displayPattern();
}
void loop() {
if (Serial.available() > 0) {
// Read user input from Serial Monitor
String input = Serial.readStringUntil('\n');
double enteredCode = input.toDouble();
Serial.print("Entered Code: ");
Serial.println(enteredCode);
// Check for unauthorized access
if (enteredCode != registerNumber) {
attempts++;
Serial.println("Incorrect Code. Attempt " + String(attempts));
unauthorizedAccess = (attempts >= 3);
} else {
attempts = 0;
unauthorizedAccess = false; // Reset unauthorized access if the code is correct
Serial.println("Code Correct. Access Granted.");
}
// Handle unauthorized access
if (unauthorizedAccess) {
blinkLED();
} else {
glowLED();
}
}
}
// Function to display the pattern on the LCD
void displayPattern() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Name: ");
lcd.print(fullName[0]);
lcd.print(".");
int lastIndex = strlen(fullName) - 7;
lcd.print(fullName[lastIndex]);
lcd.print(" - ");
// Calculate and display the last 4 digits of the registerNumber
int last_4 =(int) registerNumber % 10000;
lcd.print(last_4);
lcd.setCursor(0, 1);
for (int i = 0; i < 4; i++) {
lcd.print((int)fullName[i], HEX);
lcd.print(" ");
}
lcd.print((int)registerNumber % 100, HEX);
}
// Function to blink the LED continuously
void blinkLED() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
// Function to glow the LED steadily
void glowLED() {
digitalWrite(ledPin, HIGH);
}