I am working on a project that uses the MFRC522 RFID scanner. I have got everything wired correctly and i know it all works fine. Originally, I had it to only make a green LED come on and Buzz if the card was allowed, or if the card isn't allowed, it would make a red LED come on and buzz. That all worked fine until I tried to add on a servo that would rotate to "unlock" something and then wait 10 seconds before closing again. This is where I began to run into problems. I was lazy so I used code from an example code. That code made the servo rotate 180 degrees and then go back. My end goal was to be able to put a card to the scanner and (if it was allowed), a green LED would light up and the buzzer would go off and a servo motor would rotate allowing you to open the door. Here is my code:
//System setup:
//SDA - Pin 53
//SCK - Pin 52
//MOSI - Pin 51
//MISO - Pin 50
//RQ - N/A
//GND - Ground Pin on the "DIGITAl" pins
//RST - Pin 5
//3.3 V - 3.3 V pin on the "POWER" pins
//Card Number - 99 A2 C5 99
//Tag Number - 39 1E 03 B9
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 53
#define RST_PIN 5
#define LED_G 6 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
Servo myservo;
int pos = 0;
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(BUZZER, OUTPUT);
noTone(BUZZER);
Serial.println("Put your card to the reader...");
Serial.println();
myservo.attach(9);
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
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();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "99 A2 C5 99" )
{
Serial.println("Authorized access");
Serial.println();
delay(500);
{for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10000);
}}
digitalWrite(LED_G, HIGH);
tone(BUZZER, 500);
delay(300);
noTone(BUZZER);
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10000);
{for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
}}
}
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
}
delay(5000);
digitalWrite(LED_G, LOW);
}
else {
Serial.println(" Access denied");
digitalWrite(LED_R, HIGH);
tone(BUZZER, 300);
delay(1000);
digitalWrite(LED_R, LOW);
noTone(BUZZER);
}
}