1#include <SPI.h>
2#include <MFRC522.h>
3#include <Servo.h>
4
5#define SS_PIN 10
6#define RST_PIN 9
7MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
8Servo myServo;
9
10void setup() {
11 Serial.begin(9600); // Initialize serial communications
12 SPI.begin(); // Init SPI bus
13 mfrc522.PCD_Init(); // Init MFRC522
14 myServo.attach(3); // Servo on pin 3
15 myServo.write(0); // Initial position (Locked)
16 Serial.println("Scan your card...");
17}
18
19void loop() {
20 // Check if a new card is present
21 if ( ! mfrc522.PICC_IsNewCardPresent()) {
22 return;
23 }
24 // Select one of the cards
25 if ( ! mfrc522.PICC_ReadCardSerial()) {
26 return;
27 }
28
29 // Here you would check the UID of the card
30 // For now, any card will rotate the servo
31 Serial.println("Access Granted!");
32 myServo.write(90); // Move to 90 degrees (Unlocked)
33 delay(3000); // Wait 3 seconds
34 myServo.write(0); // Back to 0 (Locked)
35}
36