Selasa, 25 April 2017

Pemrograman kunci pintu dengan rfid

#include <EEPROM.h> // Memanggil Library EEPROM
#include <SPI.h> // Memanggil Library SPI
#include <MFRC522.h> // Memanggil Library MFRC522
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Password.h>

boolean match = false;
boolean programMode = false;
byte storedCard[4];
byte readCard[4];
byte masterCard[4] = {0xec,0x9f,0xe9,0x7}; // UID kartu yang diijinkan masuk : EC9FE97
byte masterCard1[4] = {0xec,0x9f,0xe9,0x7}; // UID kartu yang diijinkan masuk : EC9FE97
byte currentLength = 0;

Password password = Password("1234"); //inisialisasi password nya.
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); //inisialisasi pada LCD.
#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);

//karena tutorial ini menggunakan 4x4 jelas jadi kolom 4 dan baris 4
const byte ROWS = 4;
const byte COLS = 4;

//inisialisasi lampu sebagai output bila password bila maka akan menyala warna biru,
//dan bila password salah akan menyala lampu merah
const int blue = 18;

//inisialisasi umum posisi untuk setiap angka dan huruf pada keypad
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

//inisialisasi colom pada keypad. urutan nya (4,3,2,1)
// jadi nomor pin pada keypad dengan 1 masuk ke pin arduino 15.
// 2 masuk ke pin arduino 14. begitu seterusnya.
byte colPins[COLS] = {3, 2, 14, 15};


//inisialisasi colom pada keypad. urutan nya (8,7,6,5)
// jadi nomor pin pada keypad dengan 5 masuk ke pin arduino 4.
// 6 masuk ke pin arduino 5. begitu seterusnya.
byte rowPins[ROWS] = {7, 6, 5, 4};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(blue, OUTPUT);
  //sumber diambil pada examples di library password.
  keypad.addEventListener(keypadEvent);
  lcd.begin(16,2);
  delay(1000);
 
 
}

void loop(){
 
    int successRead;
  do
  {
    successRead = getID();
  }
 
  while (!successRead);
 
  if (isMaster(readCard)) // Jika kartu yang di didekatkan ke pembaca adalah MasterCard (EC9FE97) Maka akan muncul :
  {
    Serial.println("Anda diizinkan untuk masuk!");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
      lcd.setCursor(0,0);  
      lcd.print("SILAHKAN");
      lcd.setCursor(0,1);
      lcd.print("ANDA DIIZINKAN!!");
 }
  else // Jika kartu yang didekatkan ke pembaca BUKAN MasterCard (EC9FE97) Maka akan muncul :
  {
    Serial.println("Anda DILARANG MASUK !!");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
      lcd.setCursor(0,0);  
      lcd.print("MAAF");
      lcd.setCursor(0,1);
      lcd.print("ANDA DILARANG!!!");

  }
  lcd.setCursor(0,0);
  lcd.print("Pass:");
  keypad.getKey();;
}


void checkPassword(){
   if(password.evaluate()){
      digitalWrite(blue, HIGH);
      lcd.clear();
      lcd.print("Success ...");
   } else {
      digitalWrite(blue, LOW);
      lcd.clear();
      lcd.print("Wrong ...");
   }
}
 
void keypadEvent(KeypadEvent eKey){
 
  switch (keypad.getState()){
    case PRESSED:
  lcd.setCursor(0,1);
        lcd.print(eKey);
  switch (eKey){
    case '*': checkPassword(); lcd.clear(); currentLength=0;break;
    case '#': password.reset(); lcd.clear();currentLength=0; break;
    default:// password.append(eKey);
          password << eKey;
          currentLength++;
       
        //Print some feedback.
        lcd.setCursor(0,0);
        lcd.print("Pass: ");
        for (byte i=0; i<currentLength; i++){
            lcd.print('*');
        }      
   }
  }
}

int getID()
{
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return 0;
  }
 
  Serial.print("NOMOR ID ANDA ADALAH : ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
}

void readID( int number ) {
  int start = (number * 4 ) - 3;
  for ( int i = 0; i < 4; i++ ) {
    storedCard[i] = EEPROM.read(start+i);
  }
}

boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != NULL )
    match = true;
  for ( int k = 0; k < 4; k++ ) {
    if ( a[k] != b[k] )
      match = false;
  }
  if ( match ) {
    return true;
  }
  else  {
    return false;
  }
}

boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  if ( checkTwo( test, masterCard1 ) )
    return true;
  else
    return false;
}

Tidak ada komentar:

Posting Komentar