Smart Lock: Appropriate code for 3 modules: servo, RFID, Ultrasonic sensor

Hi,

I am an beginner with an Arduino, I am trying to make a project with 3 modules: RFID Module, Servo and Ultrasonic sensor. I want to make a “safe”, which is normally locked, but when we read the card on RFID module the servo open the lock, then we can open a safe. When we close it, the ultrasionic sensor, which is inside the safe, read that the doors are closed and the servo close the lock. I make a code for servo and RFID module, but when I try to add ultrasonic sensor it is not working properly. Could you tell my what type of command should I use? I was looking for the answer at the internet, but I didn’t find it :confused: I attached the code below.

In this code RFID read the proper card and the servo open the lock, after 5 seconds servo close the lock, but I want to close the lock when I close the doors, so when the distance on ultrasonic sensor shows, for example, 1cm.

Code:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 #define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;

#define trigPin 7
#define echoPin 6
 
void setup() 
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}
void loop() 
{
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;

    
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(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) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    delay(300);
    myServo.write(180);
    delay(5000);
    myServo.write(0);  
    }
 
 else   {
    Serial.println(" Access denied");
    delay(1000);
  }
}

Safe_code.ino (1.52 KB)

Shit-can that stupid example code you started with. That is the worst example code on the internet.

Instead of shrieking in horror (returning) every time something isn't perfect, continue with that part of the code when things are right.

If there is not a tag present, that code exits loop(), without ever having used the value read from the ultrasonic sensor.

That code opens the lock, waits 5 seconds and closes it. That is not what you said it should do. Why haven't you fixed that?

Why, if the wrong tag is presented, do you need to wait ANY time before being able to present the right tag?

I know thats not what I said should do. I can't fix it because I don't know how, yesterday I erase the part of the code when the servo close the lock and then I was trying with the command "If" but it didn't work.

About the the waiting time before be able to present the right tag, why not ? It spoils or bothers the system? RFID is working like it should. I need help with adding the ultrasonic sensor to the code.

I also try to do something, which is firstly based on ultrasonic sensor, but it is not working at all.

#include <Servo.h>
#define trigPin 7
#define echoPin 6
Servo myServo;

#define SS_PIN 10
#define RST_PIN 9
#include <SPI.h>
#include <MFRC522.h>
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myServo.attach(8);

myServo.write(0);
Serial.println("Put your card to the reader...");
Serial.println();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {

 if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(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();

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) {
Serial.println("the distance is less than 4");
delay(2000);
myServo.write(0);

}else if (content.substring(1) == "F2 00 7C 1B") {
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    delay(300);
    myServo.write(180);
    delay(5000);
    myServo.write(0);  
    }
}

}

but it is not working at all.

Because that code is crap.

Create a function, lockDoorIfClosed(). It should return nothing. Can you figure out how to do that?

Can you guess what that function should do? Stuff like reading the ping sensor, comparing the distance to some threshold, and making the servo move, if appropriate?

Can you figure out where, in that crappy code you started with, to call that function?

You mean the function “void lockDoorIfClosed()”. This function should move servo to position “0”, when the doors are closed. The move should be done, when the ultrasonic sensor shows, for example less than 4 cm.

If we are using the first code, which I showed. The function should be applied in the void loop() function at the end? Can you show me how does this function should look like?

I made something like that: (the doors open but doesn’t close)

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 #define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;

#define trigPin 7
#define echoPin 6
 
void setup() 
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}
void loop() 
{
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;

    
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(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) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);  
    }
 
 else   {
    Serial.println(" Access denied");
    delay(1000);
  }
void lockDoorIfClosed();
}
void lockDoorIfClosed(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
 if (distance < 4) {
Serial.println("the distance is less than 4");
delay(2000);
myServo.write(0);
}
}
void lockDoorIfClosed();

That is NOT how to call a function.

That is not how you called pulseIn(), so why did you stick void in front of this statement?

What happens if there is no tag presented when loop() is called? Do you want to lock the door before, or after, that test happens?

The person that wrote that example really should have his/her Arduino taken away, along with his/her programming license.

I want to lock the door after that test happens. Could you show me how it should look like? And explain it. I am sorry, If I am asking for too much.

I want to lock the door after that test happens.

You want to lock the door after the fact that there is no card present has been determined?

The best thing you can do with that code is start over with the example code.

Rename loop() to checkTag(), add a loop() function, and call checkTag() from it. Then all those ^&%#_^& return statements won’t be such a problem.

Then, add another function, getDistance(), where you read the distance from the sensor.

Then, add another function, unlockDoor(), where you unlock the door if the right tag was read.

The loop() function should be very short.

boolean doorLocked = true;
boolean tagRead = false;
void loop()
{
   int distanceToDoor = getDistance();
   if(distanceToDoor < doorClosedDistance)
   {
      if(!doorLocked)
      {
         lockDoor();
         doorLocked = true;
      }
   }

   readTag();
   if(tagRead)
   {
      if(checkTag())
      {
         openDoor();
         doorLocked = false;
      }
      tagRead = false;
   }
}

You should be able to determine which of the code you have now goes in which function in this structure.

So, I have to avoid this return statements or add them to loop function? getDistance() function should look like the function loop in this example code?

int trigPin = 11;    // Trigger
int echoPin = 12;    // Echo
long duration, cm, inches;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  cm = (duration/2) / 29.1;     
  inches = (duration/2) / 74;
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(250);
}

And unlockDoor() function should by something like that?

  if (content.substring(1) == "XX XX XX XX")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);  
    }
 
 else   {
    Serial.println(" Access denied");
  }

So, I have to avoid this return statements or add them to loop function?

Avoid them. Write code, indented as deep as necessary, to handle data.

Instead of:

  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }

write the code like this:

  if (mfrc522.PICC_IsNewCardPresent())
  {
     if (mfrc522.PICC_ReadCardSerial())
     {
        Serial.print("UID tag :");
        String content= "";
        byte letter;
        for (byte i = 0; i < mfrc522.uid.size; i++)
        {
           Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
           Serial.print(mfrc522.uid.uidByte[i], HEX);
           content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
           content.concat(String(mfrc522.uid.uidByte[i], HEX));
        }
     }
  }

This way, you can add more code to the end of loop(), instead of having to do everything before reading the tag.

getDistance() function should look like the function loop in this example code?

int getDistance()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);
 
  int cm = (duration/2) / 29.1;     
  return cm;
}

And unlockDoor() function should by something like that?

Some of that code should be in checkTag() and some should be in openDoor().

The openDoor() function should be concerned ONLY with opening the door. It should NOT care if the door is supposed to be opened, or not.

checkTag() should have nothing in it that affects and doors or servos. It should ONLY care whether the tag that was presented is the right tag.

Okey, I begin to understand what’s going on, thank You. One more thing, What about “doorClosedDistance”, what should it be? Is this a border value? When the value of the distanceToDoor is less than this border value the doors are locked. It can by for example 3-4 cm, right ?

I have an error with this code:

/var/folders/dj/q3q9f0dj33z8rk59w3lsstm40000gn/T//ccHJ94Qg.ltrans0.ltrans.o: In function `loop':
/Users/mateuszkaminski/Desktop/Arduino_forum_lock/Arduino_forum_lock.ino:43: undefined reference to `readTag()'
collect2: error: ld returned 1 exit status
exit status 1

Error compiling for board Arduino/Genuino Uno.

Now, The code starts to look like this:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;
#define trigPin 7
#define echoPin 6

boolean doorLocked = true;
boolean tagRead = false;

void setup(){
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop()
{
   int doorClosedDistance = 4;
   int getDistance();
   int readTag();
   int checkTag();
   int lockDoor();
   int openDoor();
   int distanceToDoor = getDistance();
   if(distanceToDoor < doorClosedDistance)
   {
      if(!doorLocked)
      {
         lockDoor();
         doorLocked = true;
      }
   }

   readTag();
   if(tagRead)
   {
      if(checkTag())
      {
         openDoor();
         doorLocked = false;
      }
      tagRead = false;
   }

}

int getDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);
 
  int cm = (duration/2) / 29.1;     
  return cm;
}

int openDoor(){
    myServo.write(180);}
int lockDoor(){
    myServo.write(0);}


void checkTag(){
String content= "";
if (content.substring(1) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);  
    }
 
 else   {
    Serial.println(" Access denied");
  }

void readTag();{
 if (mfrc522.PICC_IsNewCardPresent())
 {
   if (mfrc522.PICC_ReadCardSerial())
    {
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
    }
 }
}
}

In the function definition try removing the ; after int readDistance(). And you may have similar problems with other functions.

And you'll definitely have a problem with checkTag() because you've declared it as a void and you try to use it in an if statement as if it returns true/false.

Steve

So the function checkTag() should be “int” instead of “void”?

I corrected my code and now I have an error like this:

/var/folders/dj/q3q9f0dj33z8rk59w3lsstm40000gn/T//ccH6TkFQ.ltrans0.ltrans.o: In function `loop':
/Users/mateuszkaminski/Desktop/Arduino_forum_lock/Arduino_forum_lock.ino:43: undefined reference to `readTag()'
collect2: error: ld returned 1 exit status
exit status 1

Error compiling for board Arduino/Genuino Uno.

And the code looks like this:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;
#define trigPin 7
#define echoPin 6

boolean doorLocked = true;
boolean tagRead = false;

void setup(){
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop()
{
   int doorClosedDistance = 4;
   int getDistance();
   int readTag();
   int checkTag();
   int lockDoor();
   int openDoor();
   int distanceToDoor = getDistance();
   if(distanceToDoor < doorClosedDistance)
   {
      if(!doorLocked)
      {
         lockDoor();
         doorLocked = true;
      }
   }

   readTag();
   if(tagRead)
   {
      if(checkTag())
      {
         openDoor();
         doorLocked = false;
      }
      tagRead = false;
   }

}

int getDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);
 
  int cm = (duration/2) / 29.1;     
  return cm;
}

int openDoor(){
    myServo.write(180);}
int lockDoor(){
    myServo.write(0);}


int checkTag(){
String content= "";
if (content.substring(1) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);  
    }
 
 else   {
    Serial.println(" Access denied");
  }

int readTag();{
 if (mfrc522.PICC_IsNewCardPresent())
 {
   if (mfrc522.PICC_ReadCardSerial())
    {
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
    }
 }
}
}

What did I say about semicolons in function definitions?

And checkTag() type should really be bool or boolean. Then it needs to return true/false values.

Steve

I’m sorry, I did not notice that “;” in function. Now, there is no errors, when I check the code. Can you show me how the function checkTag() should be bool or boolean?

The code update:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;
#define trigPin 7
#define echoPin 6

boolean doorLocked = true;
boolean tagRead = false;

void setup(){
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop()
{
   int doorClosedDistance = 4;
   int getDistance();
   void readTag();
   int checkTag();
   int lockDoor();
   int openDoor();
   int distanceToDoor = getDistance();
   if(distanceToDoor < doorClosedDistance)
   {
      if(!doorLocked)
      {
         lockDoor();
         doorLocked = true;
      }
   }

   readTag();
   if(tagRead)
   {
      if(checkTag())
      {
         openDoor();
         doorLocked = false;
      }
      tagRead = false;
   }

}

int getDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);
 
  int cm = (duration/2) / 29.1;     
  return cm;
}

int openDoor(){
    myServo.write(180);}
int lockDoor(){
    myServo.write(0);}


int checkTag(){
String content= "";
if (content.substring(1) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);  
    }
 
 else   {
    Serial.println(" Access denied");
  }
}

void readTag(){
 if (mfrc522.PICC_IsNewCardPresent())
 {
   if (mfrc522.PICC_ReadCardSerial())
    {
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
    }
 }
}

mrstick1225:
Can you show me how the function checkTag() should be bool or boolean?

Just change int to bool. Then where you know the tag is o.k. put a "return true;" in and if it's not o.k. put "return false;" in.

Steve

Okey, I changed it. And I added return command. But it is not working, when I use serial port monitor there is an information to scan the proper card. When I scan it, the code of the card shows up, but nothing happen.

Serial port Monior after scanning the proper card:

Put your card to the reader...

UID tag : F2 00 7C 1BUID tag : F2 00 7C 1BUID tag : F2 00 7C 1BUID tag : F2 00 7C 1B

The checkTag function:

bool checkTag(){
String content= "";
if (content.substring(1) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);
    return true;
    }
 
 else   {
    Serial.println(" Access denied");
    return false;
  }

  
}

I'm not sure exactly what you're trying to do but in checkTag() you set the local String 'content' to nothing and then you check to see if part of it is F2 etc. It isn't, you've just set it to nothing.

I have a feeling that 'content' should be global (defined outside any function so that any function can use it). Instead you've defined two different versions of it in readTag() and checkTag() and they can't see one another.

Steve

Alright, I did that. I made a global content. Now the serial port monitor is displaying the word “Message” all the time, when I scan the card the message displays the card number. Still nothing happen. It seems to me, that the program is not using the checkTag() function.

The serial port monitor:

Message : Put your card to the reader...
Message : 
Message : 
Message : 
Message : UID tag : F2 00 7C 1B
Message : 
Message : UID tag : F2 00 7C 1B
Message : 
Message :

The improved code:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;
#define trigPin 7
#define echoPin 6

boolean doorLocked = true;
boolean tagRead = false;

String content= "";

void setup(){
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Put your card to the reader...");
  Serial.println();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop()
{
   void readTag();
   bool checkTag();
   int doorClosedDistance = 4;
   int getDistance();
   int lockDoor();
   int openDoor();
   int distanceToDoor = getDistance();

   if(distanceToDoor < doorClosedDistance)
   {
      if(!doorLocked)
      {
         lockDoor();
         doorLocked = true;
      }
   }

   readTag();
   if(tagRead)
   {
      if(checkTag())
      {
         openDoor();
         doorLocked = false;
      }
      tagRead = false;
   }

}

int getDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);
  int cm = (duration/2) / 29.1;     
  return cm;
}

int openDoor(){
    myServo.write(180);}
int lockDoor(){
    myServo.write(0);}

bool checkTag(){
if (content.substring(1) == "F2 00 7C 1B")
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    myServo.write(180);
    delay(5000);
    return true;
    }
 
 else   {
    Serial.println(" Access denied");
    return false;
  }
  
  
}
void readTag(){
 if (mfrc522.PICC_IsNewCardPresent())
 {
   if (mfrc522.PICC_ReadCardSerial())
    {
      Serial.print("UID tag :");
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(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();   
}