#include <Keypad.h>
#define relay 2
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
String inputPassword = ""; //for input password
const String Passwordbenar[] = {"1234", "147"}; // passwrod yang benar
const int jumlahPassword = sizeof(Passwordbenar) / sizeof(Passwordbenar[0]); //for jumblah password lebih dari 1
int salahCount = 0; // buat max password
const int maxSalah = 3; // buat max password
byte rowPins[ROWS] = {13, 12, 14, 27}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 25, 33, 32}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(relay, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
if (key){
passwordinput(key);
}
}
void passwordinput(char key){
if (key == '#') { // Cek jika tombol '#' ditekan
if (cekPassword(inputPassword)) {
Serial.println("Password benar!");
salahCount = 0;
} else {
salahCount++;
Serial.print("password salah ");
if (salahCount >= maxSalah){
Serial.print("3 kali percobaan");
digitalWrite(relay, HIGH);
delay(5000);
digitalWrite(relay, LOW);
salahCount = 0;
}
else {
digitalWrite(relay, LOW);
}
}
delay(2000);
inputPassword = ""; // Reset input password
Serial.println("Masukkan password:");
return;
}
else {
inputPassword += key;
Serial.println(inputPassword);
}
}
bool cekPassword(String input) {
for (int i = 0; i < jumlahPassword; i++) {
if (input == Passwordbenar[i]) {
return true;
}
}
return false;
}