#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// ================= PIN =================
#define LED_PIN 2
#define BUZZER_PIN 4
#define SERVO_PIN 18
// ================= LCD =================
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ================= SERVO =================
Servo myServo;
// ================= KEYPAD =================
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] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ================= PASSWORD =================
String correctPassword = "1234";
String inputPassword = "";
// ================= SETUP =================
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
myServo.attach(SERVO_PIN);
myServo.write(0); // posisi terkunci
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART LOCK");
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
digitalWrite(LED_PIN, HIGH); // LOCK = LED ON
}
// ================= LOOP =================
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
checkPassword();
}
else if (key == '*') {
resetInput();
}
else {
inputPassword += key;
lcd.setCursor(inputPassword.length() - 1, 1);
lcd.print("*");
}
}
}
// ================= FUNCTION =================
void checkPassword() {
lcd.clear();
if (inputPassword == correctPassword) {
lcd.print("ACCESS GRANTED");
unlockDoor();
} else {
lcd.print("WRONG PASSWORD");
alarm();
}
delay(2000);
resetInput();
}
void resetInput() {
inputPassword = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter PIN:");
}
void unlockDoor() {
digitalWrite(LED_PIN, LOW); // LED OFF = unlock
myServo.write(90); // buka
delay(3000);
myServo.write(0); // kunci lagi
digitalWrite(LED_PIN, HIGH);
}
void alarm() {
for (int i = 0; i < 3; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4