SmartLock


/*
  
  The circuit LCD:
 * LCD RS pin to digital pin 13
 * LCD Enable pin to digital pin 12
 * LCD D4 pin to digital pin 11
 * LCD D5 pin to digital pin 10
 * LCD D6 pin to digital pin 9
 * LCD D7 pin to digital pin 8
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 */



#include <SoftwareSerial.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

unsigned char key,menu,urut;
unsigned char pw1,pw2,pw3,pw4,pw5,pw6,i;
unsigned char pw_kpd1, pw_kpd2, pw_kpd3, pw_kpd4, pw_kpd5, pw_kpd6;
unsigned char cek,cnt;
unsigned char fkpd; 
char pw[7],pw_kpd[7];
char s,t;  

const int rxpin = 6; 
const int txpin = 7;

SoftwareSerial Bluetooth(rxpin, txpin); 

//----------------------------INISIALISASI KONEKSI LCD 2X16
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

void setup(){  
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  digitalWrite(4,0);
  digitalWrite(5,0);
  Bluetooth.begin(9600);
  lcd.begin(16, 2);
  lcd.print("  ASEP HERMAWAN");
  lcd.setCursor(4,1);
  lcd.print("SMARTLOCK");
  delay(10000);
//---------------------cek alamat 0
  cek=EEPROM.read(0);  
if(cek==0xFF){
   cek=1;   
   EEPROM.write(0,'1');     
   pw1='1'; EEPROM.write(1,'1');
   pw2='1'; EEPROM.write(2,'1');
   pw3='1'; EEPROM.write(3,'1');
   pw4='1'; EEPROM.write(4,'1');
   pw5='1'; EEPROM.write(5,'1');
   pw6='1'; EEPROM.write(6,'1');
   }
else{
   pw1=EEPROM.read(1);
   pw2=EEPROM.read(2);
   pw3=EEPROM.read(3);
   pw4=EEPROM.read(4);
   pw5=EEPROM.read(5);
   pw6=EEPROM.read(6);
   }
fkpd=0; 
delay(200);  
}

void loop()
{
//----------------------------- display time
  digitalWrite(5,1);
  lcd.clear();
  lcd.print("Silakan kunci...");
  lcd.setCursor(0,1);
  lcd.print("Lock Send -> L");
  Bluetooth.println("Silakan kunci, Lock Send -> L");
  fkpd=2;
  goBluetooth();
  lcd.clear();
  lcd.print("Pintu Terkunci");
  delay(3000); 
pass:
  lcd.clear();
  lcd.print("Masukan Password");
  Bluetooth.println("Masukan Password");
  lcd.setCursor(0,1);
  fkpd=1;
  menu=0;
  goBluetooth();
  if(menu==4) goto pass;
}
  

//---------------------------- keypad 
void goBluetooth(){
do{
  if(Bluetooth.available())key=Bluetooth.read();
  if(key != NO_KEY && key!='\n' && key!='\r'){
    delay(100);    
    if(key=='E' && fkpd==1){  
     lcd.clear();
     lcd.print("Ganti Password");
     lcd.setCursor(0,1);
     lcd.print("PW lama=");  
     Bluetooth.println("Ganti Password, Masukkan Password lama");
      menu=1;
      delay(300); 
    }  
    
   else if(key=='L' && fkpd==2){
      delay(250);
      fkpd=0;
      Bluetooth.println("Pintu terkunci"); 
      digitalWrite(11,0);
     }
  
 
   else if ((key=='C')&&(fkpd == 1)){  //C
      Bluetooth.println("Cancel"); 
      if(menu==3){          
         lcd.clear();         
         lcd.print("PW baru:"); 
         menu=2; 
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         }
      else if(menu==2){         
         menu=4;
         fkpd=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         } 
      else if((menu==1)||(menu==0)){
         menu=4;
         fkpd=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         }
      delay(1000);
      }
      
   else if (key=='S'&& menu==3){
      lcd.clear();
      lcd.print("Simpan PW Baru..");    
      EEPROM.write(1,pw_kpd[0]);
      EEPROM.write(2,pw_kpd[1]);
      EEPROM.write(3,pw_kpd[2]);
      EEPROM.write(4,pw_kpd[3]);
      EEPROM.write(5,pw_kpd[4]);
      EEPROM.write(6,pw_kpd[5]);
      EEPROM.write(0,1);
      pw1 = pw_kpd[0];
      pw2 = pw_kpd[1];
      pw3 = pw_kpd[2];
      pw4 = pw_kpd[3];
      pw5 = pw_kpd[4];
      pw6 = pw_kpd[5];
      
      fkpd = 0;
      for(t=0;t<6;t++)pw_kpd[t]=' ';
      Bluetooth.println("Simpan Password Baru...");
      delay(3000);
      menu = 4;
     }
      
    else if(menu<2 && fkpd==1 && key > 47 && key < 59){       
       cek_password();
    }
    else if(menu==2 && fkpd==1 && key > 47 && key < 59){
       entri_password();         
    }
 } 
key=NO_KEY;
}
 while(fkpd);
}

void cek_password(){   
   pw_kpd[cnt]=key;
   cnt++;                           
   lcd.print('*');    
   Bluetooth.print('*');             
//   lcd.print(char(key));  
   if((cnt==6)&&(menu==0)){
     Bluetooth.end();
     delay(500);
      if((pw1==pw_kpd[0])&&(pw2==pw_kpd[1])&&(pw3==pw_kpd[2])  &&(pw4==pw_kpd[3])&&(pw5==pw_kpd[4])&&(pw6==pw_kpd  [5])){
         lcd.clear();
         lcd.print("Password Benar");
         lcd.setCursor(0,1);
         lcd.print("Silakan Masuk");
         
         Bluetooth.println("Password Benar, Silakan Masuk");
         
         digitalWrite(4,1);
         digitalWrite(5,1);
         delay(200);
         digitalWrite(4,0);
         delay(200);
         digitalWrite(4,1);
         delay(200);
         digitalWrite(4,0);
         cnt=0;
         fkpd=0;
         s=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         delay(3000);
         }
      else {
         lcd.clear();
         lcd.print("Password Salah");
         Bluetooth.println("Password Salah");
         s++;
         if(s < 3){
            digitalWrite(4,1);
            delay(2000);
            digitalWrite(4,0);
            lcd.clear();
            lcd.print("Masukan Password");
            Bluetooth.println("Masukan Password");
            lcd.setCursor(0,1);
            cnt=0;
            for(t=0;t<6;t++)
            pw_kpd[t]=' ';
            }
         else if (s==3){
            lcd.clear();
            lcd.print("Anda 3x Salah");
            lcd.setCursor(0,1);
            lcd.print("Tunggu 5 Menit");
            Bluetooth.println("Anda 3x Salah, Tunggu 5 Menit");
            for(i=0;i<75;i++){
               digitalWrite(4,1);
               delay(2000);
               digitalWrite(4,0);
               delay(2000);
               }                    
            lcd.clear();
            lcd.print("Masukan Password");
            Bluetooth.println("Masukan Password");
            lcd.setCursor(0,1);
            cnt=0;
            s=0;
            for(t=0;t<6;t++)pw_kpd[t]=' ';
            }
         }
        Bluetooth.begin(9600);
      }
   else if((cnt==6)&&(menu==1)){
      Bluetooth.end();
      if((pw1==pw_kpd[0])&&(pw2==pw_kpd[1])&&(pw3==pw_kpd[2])&&(pw4==pw_kpd[3])&&(pw5==pw_kpd[4])&&(pw6==pw_kpd[5])){
         lcd.clear();
         lcd.print("Password Benar");
         Bluetooth.println("Password Benar");
         lcd.setCursor(0,1);
         delay(2000);
         lcd.clear();
         lcd.print("PW baru:");
         Bluetooth.println("Masukkan Password baru");
         cnt=0;
         menu=2;
         s=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         }
      else {
         s++;
  if(s < 3){            
     lcd.clear();
     lcd.print("Password Salah");
     Bluetooth.println("Password Salah");
     cnt=0;
     delay(2000);
     lcd.clear();
     lcd.print("Ganti Password");
     lcd.setCursor(0,1);
     lcd.print("PW lama=");
     Bluetooth.println("Ganti Password, Masukkan Password lama");
     for(t=0;t<6;t++)pw_kpd[t]=' ';
 }
         else if (s==3){
            lcd.clear();
            lcd.print("Anda 3x Salah");
            lcd.setCursor(0,1);
            lcd.print("Tunggu 5 Menit");
            Bluetooth.println("Anda 3x Salah, Tunggu 5 Menit");
            for(i=0;i<75;i++){
               digitalWrite(4,1);
               delay(2000);
               digitalWrite(4,0);
               delay(2000);
               }                    
            lcd.clear();
            lcd.print("Ganti Password");
            lcd.setCursor(0,1);
     lcd.print("PW lama=");
     Bluetooth.println("Ganti Password, Masukkan Password lama");
            cnt=0;
            s=0;
            for(t=0;t<6;t++)pw_kpd[t]=' ';
            }
      }
      Bluetooth.begin(9600);
   }    
}

void entri_password(){
   lcd.print(char(key));
   Bluetooth.print(char(key));
   pw_kpd[cnt]=key;
   cnt++;   
   if(cnt==6){
      lcd.setCursor(0,1);
      lcd.print("S=save,C=cancel");
      Bluetooth.println("S=save   C=cancel");
      menu=3;
      cnt=0;
      
      }
}

Comments

Popular Posts