1
2
3
4
5
6
7
8MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
9Servo servo; // Create servo object
10
11const int redPin = 9; // Red LED pin
12const int greenPin = 10; // Green LED pin
13const int bluePin = 11; // Blue LED pin
14
15void setup() {
16 Serial.begin(9600); // Initialize serial communication
17 SPI.begin(); // Initialize SPI communication
18 mfrc522.PCD_Init(); // Initialize MFRC522
19 servo.attach(6); // Attach servo to pin 6
20 pinMode(redPin, OUTPUT); // Set red LED pin as output
21 pinMode(greenPin, OUTPUT); // Set green LED pin as output
22 pinMode(bluePin, OUTPUT); // Set blue LED pin as output
23 // Turn on green LED and turn off red and blue LEDs
24 digitalWrite(redPin, LOW);
25 digitalWrite(greenPin, HIGH);
26 digitalWrite(bluePin, LOW);
27}
28
29void loop() {
30 // Check for new RFID card
31 if (mfrc522.PICC_IsNewCardPresent()) {
32 // Select the scanned card
33 MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
34 if (piccType != MFRC522::PICC_TYPE_UNKNOWN) {
35 // LED blue for 2 seconds
36 digitalWrite(bluePin, HIGH);
37 delay(2000);
38 digitalWrite(bluePin, LOW);
39 // LED red for 5 seconds
40 digitalWrite(redPin, HIGH);
41 digitalWrite(greenPin, LOW);
42 digitalWrite(bluePin, LOW);
43 // Rotate servo 180 degrees and back in the 5 seconds of being red
44 for (int i = 0; i <= 180; i++) {
45 servo.write(i);
46 delay(14);
47 }
48 for (int i = 180; i >= 0; i--) {
49 servo.write(i);
50 delay(14);
51 }
52 digitalWrite(redPin, LOW);
53 // LED blue for 2 seconds
54 digitalWrite(bluePin, HIGH);
55 delay(2000);
56 digitalWrite(bluePin, LOW);
57 // Turn on green LED
58 digitalWrite(greenPin, HIGH);
59 }
60 }
61}