//Project Security System dengan Arduino
//Tugas Ke-7 Bootcamp Arduino
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
#include <Servo.h>
/* Display */
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
Servo myservo;
char key_pressed=0;
int i=0;
char password[4];
char initial_password[4];
#define ledhju 12
//class input data eeprom
void insertData(){
EEPROM.write(0,12);
EEPROM.write(1,12);
}
//Class Buka Kunci
void bukaPintu(int val){
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
void setup() {
// put your setup code here, to run once:
myservo.attach(11);
bukaPintu(0);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.println("Input Password :");
delay(1000);
lcd.setCursor(0, 1);
insertData();
bukaPintu(0);
pinMode(ledhju, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String passw = String(EEPROM.read(0)) + String(EEPROM.read(1));
key_pressed = keypad.getKey();
if(key_pressed){
password[i++]=key_pressed;
lcd.print(key_pressed);
}
if(i==4){
delay(200);
for(int j=0;j<4;j++)
initial_password[j]=passw[j];
if((strncmp(password, initial_password,4)== 0))
{
lcd.clear();
lcd.print("Password Benar");
bukaPintu(90);
digitalWrite(ledhju, LOW);
delay(5000);
lcd.setCursor(0,1);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0,1);
delay(1000);
bukaPintu(0);
i=0;
}
else
{
lcd.clear();
lcd.print("Password Salah!");
digitalWrite(ledhju, HIGH);
lcd.setCursor(0,1);
bukaPintu(0);
delay(1000);
lcd.clear();
lcd.print("Enter Password");
//digitalWrite(ledhju, LOW);
lcd.setCursor(0,1);
i=0;
}
}
}