#include <Keypad.h>
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
// Define the keypad's keymap
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Pin assignments for rows and columns (adjust these based on your wiring)
byte pin_rows[ROW_NUM] = {23, 25, 26, 27}; // Row pins (GPIO)
byte pin_column[COLUMN_NUM] = {18, 19, 21, 22}; // Column pins (GPIO)
// Initialize the Keypad library
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// Pin assignments for LEDs
const int redLED = 32; // GPIO for Red LED
const int greenLED = 33; // GPIO for Green LED
// Predefined password
const char correctPassword[] = "12345"; // The correct password
char enteredPassword[5]; // Store the entered password
byte passwordIndex = 0;
void setup() {
Serial.begin(115200);
// Set LED pins as output
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Initially turn off both LEDs
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
}
void loop() {
char key = keypad.getKey(); // Check if a key is pressed
if (key) {
if (key == '#') {
// Check if the entered password matches the correct password
enteredPassword[passwordIndex] = '\0'; // Null-terminate the entered password string
if (strcmp(enteredPassword, correctPassword) == 0) {
Serial.println("Password Correct!");
digitalWrite(greenLED, HIGH); // Turn on green LED
digitalWrite(redLED, LOW); // Turn off red LED
} else {
Serial.println("Password Incorrect!");
digitalWrite(redLED, HIGH); // Turn on red LED
digitalWrite(greenLED, LOW); // Turn off green LED
}
passwordIndex = 0; // Reset the entered password index
delay(2000); // Wait for 2 seconds to show LED status before clearing
digitalWrite(redLED, LOW); // Turn off red LED
digitalWrite(greenLED, LOW); // Turn off green LED
} else if (key == '*') {
// Clear the entered password when '*' is pressed
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword)); // Reset entered password
} else {
// Add the key to the entered password array
if (passwordIndex < 4) {
enteredPassword[passwordIndex] = key;
passwordIndex++;
}
}
}
}