I'm looking at creating a forgettable RFID lock such that any RFID tag can lock it and then use the same tag to unlock it, but once it has been unlocked then any other RFID tag can lock it again. I have seen something similar with lockers.
So I am using an Arduino UNO, with an MFRC522 RFID reader and a servo which I am unsure of the specification at the moment. I currently have it programmed such that only one card can move the servo back and forth (unlocked and locked position) and hold that position when the card is tapped.
The part that I am struggling to find is how to take an unknown tag, read the UID, move the servo into its locked position and then having it such that only this tag can unlock it again, then once this is complete to clear the memory of the UID such that a different tag could then lock the servo.
This is my first post so I apologize if I have missed anything out if I have let me know and I will put the information below. Heres my current code for the project:
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
int greenled = 5;
int redled = 4;
Servo servo;
int flag = 1;
#define SDA_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SDA_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
servo.attach(3);
servo.write(20);
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent()){
Serial.println("no card");
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();
content.toUpperCase();
if (content.substring(1) == "B3 F4 0D 1A"){
if(flag%2 == 1){
servo.write(50);
digitalWrite(greenled, HIGH);
digitalWrite(redled, LOW);
flag++;
delay(1000);
}
else{
servo.write(120);
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
flag++;
delay(1000);
}
}
else {
}
}