#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the symbols on the buttons of the keypad
char hexaKeys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
// Initialize an instance of class Keypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const int ledPin = 12; // LED connected to pin 12
String inputCode = ""; // To store the input code
String targetCode = "14157"; // The correct code
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
digitalWrite(ledPin, LOW); // Initially turn off the LED
Serial.begin(115200);
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
Serial.println(customKey); // Print the pressed key to the Serial Monitor
inputCode += customKey; // Append the pressed key to the input code
// Check if the input code matches the target code
if (inputCode.length() == targetCode.length()) {
if (inputCode == targetCode) {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(5000); // Keep the LED on for 5 seconds
digitalWrite(ledPin, LOW); // Turn off the LED after 5 seconds
}
inputCode = ""; // Reset the input code after checking
}
}
}