#include <Keypad.h>
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputPassword = "";
String correctPassword = "1234";
const int ledGreen = 12;
const int ledRed = 11;
const int buzzerPin = 10;
void setup() {
Serial.begin(9600);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Pressed: ");
Serial.println(key);
if (key == '#') { // Tombol ENTER
if (inputPassword == correctPassword) {
Serial.println("Access Granted");
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, LOW);
tone(buzzerPin, 1000, 500); // bunyi 500ms
} else {
Serial.println("Access Denied");
digitalWrite(ledRed, HIGH);
digitalWrite(ledGreen, LOW);
tone(buzzerPin, 500, 1000); // bunyi 1000ms
}
delay(2000);
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
noTone(buzzerPin);
inputPassword = ""; // reset password
} else if (key == '*') { // RESET input password
inputPassword = "";
Serial.println("Input Cleared");
noTone(buzzerPin); // stop buzzer (jika masih bunyi)
} else {
inputPassword += key;
}
}
}