Senin, 05 Oktober 2020

Script arduino RFID

 #include <Servo.h>

#include <MFRC522.h>

#include <SPI.h>



#define SS_PIN 10

#define RST_PIN 9

const int led1=5;

const int led2=3;

MFRC522 mfrc522(SS_PIN, RST_PIN); 

Servo servo1;


#define ACCESS_DELAY 2000

#define DENIED_DELAY 1000


 

void setup() 

{

  Serial.begin(9600);   

  SPI.begin();          

  mfrc522.PCD_Init();  

  servo1.attach(4);

  pinMode(led1, OUTPUT);

  pinMode(led2, OUTPUT);

  Serial.println("Put your card to the reader...");

  Serial.println();


}

void loop() 

{

  

  if ( ! mfrc522.PICC_IsNewCardPresent()) 

  {

    return;

  }

 

  if ( ! mfrc522.PICC_ReadCardSerial())  {

    return;

  }


  Serial.println("UID tag :");

  String content= "";

  byte letter;

  for (byte i = 0; i < mfrc522.uid.size; i++) 

  {

     Serial.println(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");

     Serial.println(mfrc522.uid.uidByte[i], HEX);

     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));

     content.concat(String(mfrc522.uid.uidByte[i], HEX));

  }

  Serial.println();

  Serial.print("Message : ");

  content.toUpperCase();

  if (content.substring(1) == "87 F0 F0 A7") //change here the UID of the card

   {

    Serial.println("Authorized access");

    Serial.println();

  digitalWrite(led2, HIGH);

  delay(1000);

  digitalWrite(led2, LOW);

  servo1.write(90);

  delay(3000);

  servo1.write(180);

  delay(500);

  


  }

 

 else   {

    Serial.println(" Access denied");

    Serial.println();

    digitalWrite(led1, HIGH);

    delay(1000);

    digitalWrite(led1, LOW);

    delay(1000);

    servo1.write(180);

    delay(100);

   

delay(DENIED_DELAY);

 

  }

}