#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // set the LCD address to 0x27 for a 16 chars and 2 line display
String pad;
const byte numRows = 4;
const byte numCols = 4;
String password = "4321";
char keypressed;
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {5, 4, 3, 2};
byte colPins[numCols] = {A0, A1, A2, A3};
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad
Servo S;
int distance;
void setup() {
lcd.begin(16,2);
lcd.setCursor(0, 0);
lcd.print("Keypad");
lcd.setCursor(7, 0);
lcd.print("Test");
delay(1000);
lcd.clear();
S.attach(6);
}
void loop() {
readKeypad();
if (keypressed == '#') {
if (pad == password) {
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Access Granted");
S.write(180);
delay(1000);
S.write(0);
} else {
lcd.setCursor(0, 1);
lcd.print("Access Denied");
}
} if (keypressed == '*') {
pad = "";
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print(pad);
delay(100);
}
void readKeypad() {
keypressed = myKeypad.getKey(); //deteksi penekanan keypad
if (keypressed != '#') {
String konv = String(keypressed);
pad += konv;
}
}