Greetings, I'm trying to implement an access control system using arduino with a rc522 rfid reader. What I want is basically to command the servo to one position (open) when the id read from the tag is the "right" one and to drive it to another position (close) when the id is not allowed or there isn't any tag being read. The servo motor must stay in the open position as long as the allowed tag remains in range, and it should go back to the close position when the allowed tag is removed.
So far I have figured out how to read de id from the tags and use that information to activate a servo, the problem I'm having is that I haven't been able to command the servo for going back to the close position when the tag is removed.
other problem I'm having is when the allowed tag stays for a while in the range of detection, the rc522 keeps looking for a new tag and that appears to interfere with the functioning of the servo.
Hope you can help me.
Thanks
P.D. I'm working with arduino mega 2560 and the rc522 rfid reader, the library I'm using is the one from: GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
byte cardPresent;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
myservo.attach(11); // attaches the servo on pin 11 to the servo object
}
void loop()
{
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent())
{
if (mfrc522.PICC_ReadCardSerial())
{
String rfidUid = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
rfidUid += String(mfrc522.uid.uidByte[i], HEX);
}
if (rfidUid=="59ac809e")
{
Serial.println("ID: ");
Serial.println(rfidUid);
Serial.println("access granted");
myservo.write(130); // set servo to open position
}
else
{
Serial.println("ID: ");
Serial.println(rfidUid);
Serial.println("access denied");
myservo.write(0);
}
}
}
else
{
myservo.write(0); // set servo to close position
}
}