#include <Keypad.h>
#include <LiquidCrystal.h>
// Pin definitions
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A3, A2, A1, A0};
byte colPins[COLS] = {12, 11, 10, 9};
const byte RELAY_PIN = 13;
const byte RS = 2, EN = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7;
// Initialize objects
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
// Password
const char* password = "3443";
// Lockout variables
const byte MAX_ATTEMPTS = 5;
const unsigned long LOCKOUT_DURATION = 900000; // 15 minutes in milliseconds
byte attempts = 0;
unsigned long lockoutStart = 0;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Enter Passcode:");
// Initialize relay
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
static String input = "";
// Check for lockout
if (attempts >= MAX_ATTEMPTS && (millis() - lockoutStart) < LOCKOUT_DURATION) {
lcd.clear();
lcd.print("Locked Out");
return;
}
// Reset lockout if duration has elapsed
if (attempts >= MAX_ATTEMPTS && (millis() - lockoutStart) >= LOCKOUT_DURATION) {
attempts = 0;
lcd.clear();
lcd.print("Enter Passcode:");
}
// Get key from keypad
char key = keypad.getKey();
if (key) {
// Display key on LCD
lcd.setCursor(input.length(),1);
lcd.print(key);
// Add key to input
input += key;
}
// Check if passcode entered
if (input.length() == strlen(password)) {
// Compare input to password
if (input == password) {
attempts = 0;
lcd.clear();
lcd.print("Toegang verleend");
digitalWrite(RELAY_PIN, HIGH); // Activate relay
delay(10000); // Wait 10 seconds
digitalWrite(RELAY_PIN, LOW); // Deactivate relay
lcd.clear();
lcd.print("Enter Passcode:");
} else {
attempts++;
if (attempts < MAX_ATTEMPTS) {
lcd.clear();
lcd.print("Geen Toegang");
delay(1000);
lcd.clear();
lcd.print("Enter Passcode:");
} else {
lockoutStart = millis();
lcd.clear();
lcd.print("Locked Out");
}
}
// Reset input
input = "";
}
}