Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <deprecated.h>
- #include <MFRC522.h>
- #include <MFRC522Extended.h>
- #include <require_cpp11.h>
- #define SS_PIN 10
- #define RST_PIN 9
- MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
- MFRC522::MIFARE_Key key;
- int code[2][4] = {{103,136,18,82}, {107,108,60,10}}; //This is the stored UID
- int codeRead = 0;
- String uidString;
- const int ledPin = 3;
- const int ledOnTime = 1000;
- int servoPin = 5;
- Servo Servo1;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- SPI.begin(); // Init SPI bus
- rfid.PCD_Init(); // Init MFRC522
- Servo1.attach(servoPin);
- pinMode(ledPin, OUTPUT);
- Servo1.write(0);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if( rfid.PICC_IsNewCardPresent())
- {
- readRFID();
- }
- delay(100);
- }
- void openServo()
- {
- Servo1.write(90);
- Serial.println("open Servo");
- delay(3000);
- closeServo();
- }
- void closeServo()
- {
- Servo1.write(0);
- Serial.println("close Servo");
- delay(1000);
- }
- void readRFID()
- {
- rfid.PICC_ReadCardSerial();
- Serial.print(F("\nPICC type: "));
- MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
- Serial.println(rfid.PICC_GetTypeName(piccType));
- // Check is the PICC of Classic MIFARE type
- if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
- piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
- piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
- Serial.println(F("Your tag is not of type MIFARE Classic."));
- return;
- }
- Serial.println("Scanned PICC's UID:");
- printDec(rfid.uid.uidByte, rfid.uid.size);
- uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);
- int i = 0;
- boolean match = true;
- while(i<2)
- {
- int j = 0;
- boolean check = true;
- while(j<rfid.uid.size)
- {
- if(!(rfid.uid.uidByte[j] == code[i][j]))
- {
- check = false;
- break;
- }
- j++;
- }
- if(check)
- {
- break;
- }
- i++;
- }
- if(match)
- {
- Serial.println("\nI know this card!");
- digitalWrite(ledPin, HIGH);
- openServo();
- //delay(ledOnTime);
- digitalWrite(ledPin, LOW);
- }else
- {
- Serial.println("\nUnknown Card");
- }
- // Halt PICC
- rfid.PICC_HaltA();
- // Stop encryption on PCD
- rfid.PCD_StopCrypto1();
- }
- void printDec(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i], DEC);
- Serial.print(" ");
- //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- //Serial.print(buffer[i], DEC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement