#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define RC522_SS_PIN 10 // The Arduino Nano pin connected to RC522's SS pin
#define RC522_RST_PIN 5 // The Arduino Nano pin connected to RC522's RST pin
#define SERVO_PIN A5 // The Arduino Nano pin connected to servo motor
MFRC522 rfid(RC522_SS_PIN, RC522_RST_PIN);
Servo servo;
byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF};
int angle = 0; // The current angle of servo motor
void setup() {
Serial.begin(9600);
SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522
servo.attach(SERVO_PIN);
servo.write(angle); // rotate servo motor to 0°
Serial.println("Tap RFID/NFC Tag on reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (rfid.uid.uidByte[0] == authorizedUID[0] &&
rfid.uid.uidByte[1] == authorizedUID[1] &&
rfid.uid.uidByte[2] == authorizedUID[2] &&
rfid.uid.uidByte[3] == authorizedUID[3] ) {
Serial.println("Authorized Tag");
// change angle of servo motor
if (angle == 0)
angle = 90;
else //if(angle == 90)
angle = 0;
// rotate the servo motor to the angle position
servo.write(angle);
Serial.print("Rotate Servo Motor to ");
Serial.print(angle);
Serial.println("°");
} else {
Serial.print("Unauthorized Tag with UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}
}
}
Can you tell what is wrong with my project code pls or is it something else
That is your problem. The UID for an RFID tag is 64 bits long not 8 bits.
You need to use an unsigned long long variable to hold these not bytes.
Having then all the same value is not going to help either. You need to know the UIDs that are authorized and add then to your code.
I see nothing in your code that would allow a new card to be presented and it then would be deemed to be a valid code and then stored in some sort of none volatile storage like an SD card or EEPROM.
You will print it out using the serial print to the serial monitor window. Make sure that the baud rate of the serial monitor is set to the same serial baud rate as the print will use.
You then make a note of the number each tag gives you and include that number in your list of authorized tags.
Now have you made the changes I told you about?
Did it make a difference to your code when you ran it?
Post again your changed code, in a new reply so we can see if you understand what you have been told to do already. Or explain what you didn't understand about what you have been told.