#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
Servo servo; // Create servo object
const int redPin = 9; // Red LED pin
const int greenPin = 10; // Green LED pin
const int bluePin = 11; // Blue LED pin
void setup() {
Serial.begin(9600); // Initialize serial communication
SPI.begin(); // Initialize SPI communication
mfrc522.PCD_Init(); // Initialize MFRC522
servo.attach(6); // Attach servo to pin 6
pinMode(redPin, OUTPUT); // Set red LED pin as output
pinMode(greenPin, OUTPUT); // Set green LED pin as output
pinMode(bluePin, OUTPUT); // Set blue LED pin as output
// Turn on green LED and turn off red and blue LEDs
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}
void loop() {
// Check for new RFID card
if (mfrc522.PICC_IsNewCardPresent()) {
// Select the scanned card
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
if (piccType != MFRC522::PICC_TYPE_UNKNOWN) {
// LED blue for 2 seconds
digitalWrite(bluePin, HIGH);
delay(2000);
digitalWrite(bluePin, LOW);
// LED red for 5 seconds
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// Rotate servo 180 degrees and back in the 5 seconds of being red
for (int i = 0; i <= 180; i++) {
servo.write(i);
delay(14);
}
for (int i = 180; i >= 0; i--) {
servo.write(i);
delay(14);
}
digitalWrite(redPin, LOW);
// LED blue for 2 seconds
digitalWrite(bluePin, HIGH);
delay(2000);
digitalWrite(bluePin, LOW);
// Turn on green LED
digitalWrite(greenPin, HIGH);
}
}
}
Comments