#include <Keypad.h>
const int RELAY_PIN = 10; // Relay pin
const int ROW = 4; // Number of rows
const int COLUMN = 4; // Number of columns
// Define the keypad layout
char keys[ROW][COLUMN] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte pin_rows[ROW] = {9, 8, 7, 6}; // Row pins
byte pin_column[COLUMN] = {5, 4, 3, 2}; // Column pins
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW, COLUMN);
const String Pass = "1234"; // Set the password
String input_password;
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT); // Set relay pin as output
digitalWrite(RELAY_PIN, LOW); // Set relay to NC (Normally Closed)
Serial.println("Masukkan Pin :"); // Initial prompt
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') { // Reset password input if '*' is pressed
input_password = "";
Serial.println("Input direset");
}
else if (key == '#') { // Check password when '#' is pressed
if (input_password == Pass) {
Serial.println("PIN BENAR");
digitalWrite(RELAY_PIN, HIGH); // Activate relay
delay(5000); // Set duration
digitalWrite(RELAY_PIN, LOW); // Deactivate relay
}
else {
Serial.println("PIN SALAH");
delay(3000); // Delay after incorrect password
}
input_password = ""; // Reset input for new attempts
Serial.println("Masukkan Pin :"); // Prompt again
}
else {
input_password += key; // Add key to input
}
}
}