-
Notifications
You must be signed in to change notification settings - Fork 0
/
andrew_rfid.ino
98 lines (75 loc) · 2.31 KB
/
andrew_rfid.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#include <LiquidCrystal.h> //Liquid crystal used for optional LCD screen
#define RST_PIN 9
#define SS_PIN 10
// Define authorized cards
String MasterTag1 = "977AE2B5";
vector<String> accessCards;
// Define future string for user card
String inputCard = "";
bool adminMode = false;
//Create MFRC522 instance
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
// Initiating
SPI.begin(); //Initialize serial communications
mfrc522.PCD_Init(); //Initialize MFRC522
Serial.println("Scan your card...");
}
void loop() {
// Wait untill new tag is available
if (getID()) {
if (!adminMode) {
// user mode
for (String card : accessCards) {
if (card == inputCard) {
cout << "Access granted";
}
}
for (String card : masterCards) {
if (card == inputCard) {
adminMode = true;
// start delay
adminModeDelay();
}
}
} else {
// adminMode
cout << "new card detected";
accessCards.push(inputCard);
cout << "new card added";
adminMode = false;
}
delay(2000);
// Loop begins again and waits for card to be scanned
}
}
void adminModeDelay() {
delay(5000);
adminMode = false;
Serial.println("Admin mode disabled");
}
// Read new tag if available. If this function is true,
boolean getID(){
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
inputCard = ""; // Reset the tag to blank
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
inputCard.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
inputCard.toUpperCase();
Serial.println("User card ID presented:");
Serial.print(inputCard);
mfrc522.PICC_HaltA(); // Stop reading
return true;
}