#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#define Password_Length 6
Servo myservo;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
int pos = 0;
char Data[Password_Length];
char Master[Password_Length] = "12345";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
bool door = false;
char customKey;
int attempts = 0;
bool locked = false;
const int ledPins[] = {10, 11, 12};
const int buzzerPin = 13;
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] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
myservo.attach(9, 2000, 2400);
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], HIGH);
}
pinMode(buzzerPin, OUTPUT);
ServoClose();
lcd.begin(16, 2);
lcd.print("Pintu Dikunci");
loading("Loading");
lcd.clear();
}
void loop()
{
if (door == true)
{
customKey = customKeypad.getKey();
if (customKey == '#')
{
lcd.clear();
ServoClose();
lcd.print("Pintu Tertutup");
delay(3000);
door = false;
}
}
else if (!locked)
{
Open();
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Akses");
lcd.setCursor(0,1);
lcd.print("Dikunci");
delay(200);
tone(buzzerPin, 500, 200);
delay(200);
noTone(buzzerPin);
delay(200);
}
}
void loading (char msg[]) {
lcd.setCursor(0, 1);
lcd.print(msg);
for (int i = 0; i < 5; i++) {
delay(1000);
lcd.print(".");
}
}
void clearData()
{
while (data_count != 0)
{
Data[data_count--] = 0;
}
return;
}
void ServoClose()
{
for (pos = 90; pos >= 0; pos -= 10) {
myservo.write(pos);
}
}
void ServoOpen()
{
for (pos = 0; pos <= 90; pos += 10) {
myservo.write(pos);
}
}
void Open()
{
lcd.setCursor(0, 0);
lcd.print("Masukan Sandi");
customKey = customKeypad.getKey();
if (customKey)
{
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1)
{
if (!strcmp(Data, Master))
{
lcd.clear();
ServoOpen();
tone(buzzerPin, 1000, 500);
lcd.print(" Pintu Terbuka ");
door = true;
delay(3000);
loading("Menunggu");
lcd.clear();
lcd.print(" Waktu Habis! ");
delay(500);
ServoClose();
door = false;
attempts = 0;
clearData();
for (int i = 0; i < 3; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
else
{
attempts++;
lcd.clear();
lcd.print(" Sandi Salah ");
delay(1000);
if (attempts <= 3) {
digitalWrite(ledPins[attempts - 1], LOW);
tone(buzzerPin, 500, 500);
}
if (attempts >= 3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Akses");
lcd.setCursor(0,1);
lcd.print("Dikunci");
delay(1000);
locked = true;
}
lcd.clear();
clearData();
}
}
}