Go Down

Topic: Door access control System using RC522, arduino UNO,LCD andservo (Read 521 times) previous topic - next topic

irfan_help_me_please

Hi there, i am currently working on a door access project using rfid rc522 with LCD 16x2 and servo. My problem that i need to solve is to have the system open to any RFID card and then allow access for 6 seconds and will auto lock after that.Then the system will somehow get its ID or its information to be used again to unlock the lock when scanned the correct card the second time to retrieve things inside before auto lock again after 6 seconds. then after the process of one user using the lock, it will then have to wipe the data saved previously and restart the loop back to its state at first which is to be open to any rfid card again to use the service.

But now, my problem i am facing is the capturing of ID of the card being tapped at first to make it unlock the lock again when being tapped the second time and the wiping of data at the end of the loop to be used by other users.

Assistance in my project is much much needed and hope you guys can provide assistance to help me work out this project.

Thank you in advance :)

sterretje

Code: [Select]
if (val = 1)

There is a difference between = (assigment) and == (compare for equal).

Please read How to use this forum - please read, specifically point 7 how to post code. Your code is small enough to be embedded in a post instead of being attached.

You can save the uid bytes in another variable using memcpy(). When the card is tapped the second time (to get out), compare the uid bytes against the stored uid bytes using memcmp().

Or you can store in eeprom if it needs to be remembered after a power failure so people still can get out once power is restored.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

irfan_help_me_please

Hi , thanks for replying. Sorry i am new here and new to arduino and i am confused on what function to use to capture the IDs as you mentioned earlier. Hope you do not mind teaching me on how to input the functions you mentioned above. Below is the code.



Code: [Select]
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
 
#define SS_PIN 10
#define RST_PIN 9 
Servo myServo;               
MFRC522 mfrc522(SS_PIN, RST_PIN);    // Create MFRC522 instance.
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
 byte data[4];  //For holding the ID we receive
 int val = 0;
 byte new_card[4];
 


 
void setup()
{
  Serial.begin(19200);               // Initiate a serial communication

 
  SPI.begin();                      // Initiate  SPI bus
  mfrc522.PCD_Init();               // Initiate MFRC522
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Scan card..");
  Serial.println();
  lcd.begin(16, 2);                 // set up the LCD's number of columns and rows:
 
}


void loop()
{
  lcd.setCursor(0,0);               // set print area at the top row of the LCD
  lcd.print("Scan your card");       // display at LCD
  lcd.setCursor(0,1);               // set print area at bottom row of LCD
  lcd.print("on reader...");        // display at LCD
    while(Serial.available()>0)
   {
     Serial.read();
     val = val + 1;
   }
                                   // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("UID tag :");       // Show UID on serial monitor
  String content= "";
  byte letter;
  byte n_card = false;
 
   for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     if (mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")
     {
      Serial.print("0");
     }
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     if (mfrc522.uid.uidByte[i] != new_card[i])
     {
      n_card = false;
     }
     
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
 


  // code to allow any card to access
  // code that allows it to store the card details to be used again to unlock the door later down below (??????????)
  //
  //
  //
  //
  //
  //
  if (val = 1) //change here the UID of the card/cards that you want to give access
 
  {
   
    lcd.setCursor(0,0);
    Serial.println("Access Granted");
    lcd.print("Access Granted ");
    lcd.setCursor(0,1);
    lcd.print("Pls insert bike.");
    myServo.write(180);
    delay(6000);
    lcd.clear();
    myServo.write(0);
    delay(1000);
    lcd.setCursor(0,0);               // set print area at the top row of the LCD
    lcd.print("Scan auth card");       // display at LCD
    lcd.setCursor(0,1);               // set print area at bottom row of LCD
    lcd.print("to retrieve");        // display at LCD
    delay(2000);
  }

    else
    {
      return;
    }
   
 
   
  if (val = 2)
   {
    lcd.setCursor(0,0);
    Serial.println("Access granted");
    lcd.print(" Welcome back :)");
    lcd.setCursor(0,1);
    lcd.print("Please retrieve.");
    myServo.write(180);
    delay(6000);
    lcd.clear();
    myServo.write(0);
    delay(1000);

   
 }
 
 

 else
 {
  return;
}
}

sterretje

Code: [Select]
...
...
byte userCard[4];           // the card that the user used to place the bicycle in the locker
bool hasBicycle = false;    // if a bicycle was placed in the locker

void setup()
{
  ...
  ...
 
  // display initial message
  displayScanMessage();
}

void loop()
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("UID tag :");       // Show UID on serial monitor

  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    if (mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")
    {
      Serial.print("0");
    }
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.println();

  if (hasBicycle == false)
  {
    // save the card uid
    memcpy(userCard, mfrc522.uid.uidByte, sizeof(userCard));
    hasBicycle = true;

    lcd.setCursor(0, 0);
    Serial.println("Access Granted");
    lcd.print("Access Granted ");
    lcd.setCursor(0, 1);
    lcd.print("Pls insert bike.");

    ...
    ...

    displayScanMessage();
  }
  else
  {
    // check if the card uid matched the saved one
    if (memcmp(userCard, mfrc522.uid.uidByte, sizeof(userCard)) == 0)
    {
      lcd.setCursor(0, 0);
      Serial.println("Access granted");
      lcd.print(" Welcome back :)");
      lcd.setCursor(0, 1);
      lcd.print("Please retrieve.");
      hasBicycle = false;

      ...
      ...

      displayScanMessage();
    }
  }
}

/*
  display default message
*/
void displayScanMessage()
{
  lcd.setCursor(0, 0);            // set print area at the top row of the LCD
  lcd.print("Scan your card");    // display at LCD
  lcd.setCursor(0, 1);            // set print area at bottom row of LCD
  lcd.print("on reader...");      // display at LCD
}

A little modified from your code; I could not figure out all your variables. This should give you something to work with.

I've created a function to display the scan message (from beginning of your loop()). It's called once in setup and it's called after access to the locker. This will prevent the LCD from constantly being updated possibly resulting in flicker.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

irfan_help_me_please

Now i can do what i am suppose to do for the project but now the problem is the displayScanMessage is not presented as requested at the end of every if statement in the code but instead the lcdprint messages that is found at the top of the void loop was presented.

Code: [Select]
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
 
#define SS_PIN 10
#define RST_PIN 9 
Servo myServo;               
MFRC522 mfrc522(SS_PIN, RST_PIN);    // Create MFRC522 instance.
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
byte userCard[4];           // the card that the user used to place the bicycle in the locker
bool hasBike = false;    // if a bicycle was placed in the locker
 


 
void setup()
{
  Serial.begin(19200);               // Initiate a serial communication

 
  SPI.begin();                      // Initiate  SPI bus
  mfrc522.PCD_Init();               // Initiate MFRC522
  myServo.attach(8);
  myServo.write(0);
  Serial.println("Scan card..");
  Serial.println();
  lcd.begin(16, 2);                 // set up the LCD's number of columns and rows:


 
 
}


void loop()
{


  lcd.setCursor(0,0);               // set print area at the top row of the LCD
  lcd.print("Flash RFID card");       // display at LCD
  lcd.setCursor(0,1);               // set print area at bottom row of LCD
  lcd.print("to access ...");        // display at LCD

  while(Serial.available() >0)
  {
    Serial.read();
  }
                                   // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
   
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("UID tag :");       // Show UID on serial monitor
  String content= "";
  byte letter;
 
   for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     if (mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")
     {
      Serial.print("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 (hasBike == false)
  {
    // save the card uid
    memcpy(userCard, mfrc522.uid.uidByte, sizeof(userCard));
    hasBike = true;

    lcd.setCursor(0,0);
    Serial.println("Access Granted");
    lcd.print("Access Granted ");
    lcd.setCursor(0,1);
    lcd.print("Pls insert bike.");
    myServo.write(180);
    delay(6000);
    lcd.clear();
    myServo.write(0);
    delay(1000);
    displayScanMessage();
   
  }
   else
  {
    // check if the card uid matched the saved one
    if (memcmp(userCard, mfrc522.uid.uidByte, sizeof(userCard)) == 0)
    {
    lcd.setCursor(0,0);
    Serial.println("Access granted");
    lcd.print(" Welcome back :)");
    lcd.setCursor(0,1);
    lcd.print("Please retrieve.");
    myServo.write(180);
    delay(6000);
    lcd.clear();
    myServo.write(0);
    delay(1000);
    hasBike = false;
    }
    else {
      lcd.setCursor(0,0);
      Serial.println("Unidentified card");
      lcd.print("  Wrong card  ");
      lcd.setCursor(0,1);
      lcd.print("Pls try again!!!");
      delay(1000);
      displayScanMessage();
     
     
    }
  }
}
void displayScanMessage()
{
  lcd.setCursor(0, 0);            // set print area at the top row of the LCD
  lcd.print("Scan auth card");    // display at LCD
  lcd.setCursor(0, 1);            // set print area at bottom row of LCD
  lcd.print("on the reader.");      // display at LCD
}

sterretje

You overwrite it with
Code: [Select]
  lcd.setCursor(0, 0);              // set print area at the top row of the LCD
  lcd.print("Flash RFID card");       // display at LCD
  lcd.setCursor(0, 1);              // set print area at bottom row of LCD
  lcd.print("to access ...");        // display at LCD


PS
Forget that String (capital S) exists and use c-strings. Extensive use of String (capital S) will one of those days bite you with run time errors.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up