#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Servo.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define LedKuning 13
#define LedMerah 12
#define BuzzerPin 11
#define ServoPin 10
Servo myservo;
int pos = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
char Line1[35] = {"SISTEM KEAMANAN"};
char Line2[20] = {"by: Mala"};
char Line3[20] = {"MASUKKAN PASSWORD: "};
char Line4[20] = {" "};
char Line4a[20] = {" PASSWORD SALAH "};
char Line4b[20] = {" PASSWORD BENAR "};
int count, PosLine1;
char Password[6];
char Input_Password[6];
int count_password;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int Num;
char key;
boolean Result;
void setup() {
InputPassword();
Serial.begin(115200);
pinMode(LedKuning, OUTPUT);
pinMode(LedMerah, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
myservo.attach(10);
Inisialisasi();
ServoTutup();
lcd.init();
lcd.clear();
count = 0; PosLine1 = 0;
Num = 0;
TulisLine2();
TulisLine3();
}
void loop() {
key = keypad.getKey(); //storing pressed key value in a char
if (key!=NO_KEY)
DetectButtons();
if (Num==6) {
CheckPassword();
Execute();
Num = 0;
}
TulisLine1();
// ServoBuka(); delay(2000);
// ServoTutup(); delay(2000);
}
void Execute(){
if(Result==false){
digitalWrite(LedMerah, HIGH);
digitalWrite(BuzzerPin, HIGH);
TulisLine4a();
delay(1000);
}
if(Result==true) {
digitalWrite(LedKuning, HIGH);
TulisLine4b();
ServoBuka();
delay(1000);
ServoTutup();
}
Inisialisasi();
}
void Inisialisasi(){
digitalWrite(LedKuning, LOW);
digitalWrite(LedMerah, LOW);
digitalWrite(BuzzerPin, LOW);
TulisLine4();
}
void TulisLine1() {
int mm;
if (count == 0){
lcd.setCursor(0, 0);
for(int m=0; m<=19; m++){
mm = PosLine1 + m;
if(mm >= 35){mm = mm % 35;}
lcd.print(Line1[mm]);
}
PosLine1++;
if(PosLine1 == 35){PosLine1 = 0;}
}
count++;
if (count == 20000){count = 0;}
}
void TulisLine2() {
lcd.setCursor(0, 1);
for(int n=0; n<=19; n++){
lcd.print(Line2[n]);
}
}
void TulisLine3() {
lcd.setCursor(0, 2);
for(int n=0; n<=19; n++){
lcd.print(Line3[n]);
}
}
void TulisLine4() {
lcd.setCursor(0, 3);
for(int n=0; n<=19; n++){
lcd.print(Line4[n]);
Serial.print(Line4[n]);
}
}
void TulisLine4a() {
lcd.setCursor(0, 3);
for(int n=0; n<=19; n++){
lcd.print(Line4a[n]);
Serial.print(Line4a[n]);
}
}
void TulisLine4b() {
lcd.setCursor(0, 3);
for(int n=0; n<=19; n++){
lcd.print(Line4b[n]);
Serial.print(Line4b[n]);
}
}
void CheckPassword(){
Result=true;
for(int n=0; n<=5; n++){
if(Input_Password[n]!=EEPROM.read(n)) {Result = false;}
}
}
void DetectButtons()
{
lcd.setCursor(Num, 3);
if (key == '1') {
lcd.println ("1"); Input_Password[Num] = '1'; Num++;}
if (key == '2'){
lcd.println ("2"); Input_Password[Num] = '2'; Num++;}
if (key == '3'){
lcd.println ("3"); Input_Password[Num] = '3'; Num++;}
if (key == '4'){
lcd.println ("4"); Input_Password[Num] = '4'; Num++;}
if (key == '5'){
lcd.println ("5"); Input_Password[Num] = '5'; Num++;}
if (key == '6'){
lcd.println ("6"); Input_Password[Num] = '6'; Num++;}
if (key == '7'){
lcd.println ("7"); Input_Password[Num] = '7'; Num++;}
if (key == '8'){
lcd.println ("8"); Input_Password[Num] = '8'; Num++;}
if (key == '9'){
lcd.println ("9"); Input_Password[Num] = '9'; Num++;}
if (key == '0'){
lcd.println ("0"); Input_Password[Num] = '0'; Num++;}
}
void InputPassword(){
EEPROM.write(0, '0');
EEPROM.write(1, '6');
EEPROM.write(2, '1');
EEPROM.write(3, '9');
EEPROM.write(4, '9');
EEPROM.write(5, '9');
}
void ServoBuka(){
for (pos = 180; pos >= 90; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void ServoTutup(){
for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(15); // waits 15ms for the servo to reach the position
}
}