Advertisement
Maderdash

Untitled

Apr 20th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <RFID.h>
  3. #include <Servo.h>
  4.  
  5. RFID rfid(10, 4);       //D10:pin of tag reader SDA. D9:pin of tag reader RST
  6. unsigned char status;
  7. unsigned char str[MAX_LEN]; //MAX_LEN is 16: size of the array
  8.  
  9. String accessGranted [2] = {"310988016", "271011115139"};  //RFID serial numbers to grant access to
  10. int accessGrantedSize = 2;                                //The number of serial numbers
  11.  
  12. Servo lockServo;                //Servo for locking mechanism
  13. int lockPos = 15;               //Locked position limit
  14. int unlockPos = 400;   //Unlocked position limit
  15. bool locked = true;
  16.  
  17. int redLEDPin = 5;
  18. int greenLEDPin = 6;
  19.  
  20. void setup()
  21. {
  22.   Serial.begin(9600);     //Serial monitor is only required to get tag ID numbers and for troubleshooting
  23.   SPI.begin();            //Start SPI communication with reader
  24.   rfid.init();            //initialization
  25.   pinMode(redLEDPin, OUTPUT);     //LED startup sequence
  26.   pinMode(greenLEDPin, OUTPUT);
  27.   digitalWrite(redLEDPin, HIGH);
  28.   delay(200);
  29.   digitalWrite(greenLEDPin, HIGH);
  30.   delay(200);
  31.   digitalWrite(redLEDPin, LOW);
  32.   delay(200);
  33.   digitalWrite(greenLEDPin, LOW);
  34.   lockServo.attach(8);
  35.   lockServo.write(lockPos);         //Move servo into locked position
  36.   Serial.println("Place card/tag near reader...");
  37. }
  38.  
  39. void loop()
  40. {
  41.   if (rfid.findCard(PICC_REQIDL, str) == MI_OK)   //Wait for a tag to be placed near the reader
  42.   {
  43.     Serial.println("Card found");
  44.     String temp = "";                             //Temporary variable to store the read RFID number
  45.     if (rfid.anticoll(str) == MI_OK)              //Anti-collision detection, read tag serial number
  46.     {
  47.       Serial.print("The card's ID number is : ");
  48.       for (int i = 0; i < 4; i++)                 //Record and display the tag serial number
  49.       {
  50.         temp = temp + (0x0F & (str[i] >> 4));
  51.         temp = temp + (0x0F & str[i]);
  52.       }
  53.       Serial.println (temp);
  54.       checkAccess (temp);     //Check if the identified tag is an allowed to open tag
  55.     }
  56.     rfid.selectTag(str); //Lock card to prevent a redundant read, removing the line will make the sketch read cards continually
  57.   }
  58.   rfid.halt();
  59. }
  60.  
  61. void checkAccess (String temp)    //Function to check if an identified tag is registered to allow access
  62. {
  63.   bool granted = false;
  64.   for (int i=0; i <= (accessGrantedSize-1); i++)    //Runs through all tag ID numbers registered in the array
  65.   {
  66.     if(accessGranted[i] == temp)            //If a tag is found then open/close the lock
  67.     {
  68.       Serial.println ("Access Granted");
  69.       granted = true;
  70.       if (locked == true)         //If the lock is closed then open it
  71.       {
  72.           lockServo.write(unlockPos);
  73.           locked = false;
  74.       }
  75.       else if (locked == false)   //If the lock is open then close it
  76.       {
  77.           lockServo.write(lockPos);
  78.           locked = true;
  79.       }
  80.       digitalWrite(greenLEDPin, HIGH);    //Green LED sequence
  81.       delay(200);
  82.       digitalWrite(greenLEDPin, LOW);
  83.       delay(200);
  84.       digitalWrite(greenLEDPin, HIGH);
  85.       delay(200);
  86.       digitalWrite(greenLEDPin, LOW);
  87.       delay(200);
  88.     }
  89.   }
  90.   if (granted == false)     //If the tag is not found
  91.   {
  92.     Serial.println ("Access Denied");
  93.     for (int blinks = 0; blinks < 2; blinks++){
  94.       digitalWrite(redLEDPin, HIGH);      //Red LED sequence
  95.       delay(200);
  96.       digitalWrite(redLEDPin, LOW);
  97.       delay(200);
  98.     }
  99.   }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement