1
+ #include < SPI.h>
2
+
3
+ #include < MFRC522.h>
4
+
5
+ #define SS_PIN 10
6
+ #define RST_PIN 9
7
+ MFRC522 rfid (SS_PIN, RST_PIN);
8
+ MFRC522::MIFARE_Key key;
9
+ byte nuidPICC[4 ];
10
+ char ACCESS_GRANTED[] = " granted" ;
11
+
12
+ const int SUCCESS_PIN = 7 ;
13
+ const int ERROR_PIN = 8 ;
14
+ const int BUZZER_PIN = 6 ;
15
+
16
+ void setup () {
17
+ Serial.begin (9600 );
18
+ SPI.begin ();
19
+ rfid.PCD_Init ();
20
+ pinMode (SUCCESS_PIN, OUTPUT);
21
+ pinMode (ERROR_PIN, OUTPUT);
22
+ pinMode (BUZZER_PIN, OUTPUT);
23
+
24
+ Serial.println (F (" READING THE CARD UNIQUE ID:" ));
25
+ for (byte i = 0 ; i < 6 ; i++) {
26
+ key.keyByte [i] = 0xFF ;
27
+ }
28
+ }
29
+ void loop () {
30
+ if (!rfid.PICC_IsNewCardPresent ()) {
31
+ return ;
32
+ }
33
+ if (!rfid.PICC_ReadCardSerial ()) {
34
+ return ;
35
+ }
36
+ if (rfid.uid .uidByte [0 ] != nuidPICC[0 ] ||
37
+ rfid.uid .uidByte [1 ] != nuidPICC[1 ] ||
38
+ rfid.uid .uidByte [2 ] != nuidPICC[2 ] ||
39
+ rfid.uid .uidByte [3 ] != nuidPICC[3 ]) {
40
+ for (byte i = 0 ; i < 4 ; i++) {
41
+ nuidPICC[i] = rfid.uid .uidByte [i];
42
+ }
43
+ Serial.print (F (" [check-access]" ));
44
+ printHex (rfid.uid .uidByte , rfid.uid .size );
45
+ Serial.println (F (" \n " ));
46
+ Serial.setTimeout (500 );
47
+ String res = Serial.readString ();
48
+ res.trim ();
49
+ Serial.print (" |" );
50
+ Serial.print (res);
51
+ Serial.println (" |" );
52
+ if (res.equals (ACCESS_GRANTED)){
53
+ Serial.println (" ACCESS GRANTED" );
54
+ access_granted ();
55
+ }else {
56
+ Serial.println (" ACCESS DENIED" );
57
+ access_denied ();
58
+ }
59
+ } else {
60
+ Serial.println (F (" This car was lastly detected ." ));
61
+ buzz (" once" );
62
+ }
63
+ /*
64
+ * Halt PICC
65
+ * Stop encryption on PCD
66
+ */
67
+ rfid.PICC_HaltA ();
68
+ rfid.PCD_StopCrypto1 ();
69
+ }
70
+ void printHex (byte * buffer, byte bufferSize) {
71
+ for (byte i = 0 ; i < bufferSize; i++) {
72
+ Serial.print (buffer[i] < 0x10 ? " 0" : " " );
73
+ Serial.print (buffer[i], HEX);
74
+ }
75
+ }
76
+
77
+ void access_granted () {
78
+ digitalWrite (SUCCESS_PIN, HIGH);
79
+ digitalWrite (ERROR_PIN, LOW);
80
+ buzz (" twice" );
81
+ delay (1000 );
82
+ digitalWrite (SUCCESS_PIN, LOW);
83
+ }
84
+
85
+ void access_denied () {
86
+ digitalWrite (ERROR_PIN, HIGH);
87
+ digitalWrite (SUCCESS_PIN, LOW);
88
+ buzz (" long" );
89
+ digitalWrite (ERROR_PIN, LOW);
90
+ }
91
+
92
+ void buzz (String type){
93
+ if (type.equals (" once" )) {
94
+ analogWrite (BUZZER_PIN, 60 );
95
+ delay (60 );
96
+ analogWrite (BUZZER_PIN, 0 );
97
+ }else if (type.equals (" twice" )){
98
+ analogWrite (BUZZER_PIN, 60 );
99
+ delay (60 );
100
+ analogWrite (BUZZER_PIN, 0 );
101
+ delay (60 );
102
+ analogWrite (BUZZER_PIN, 60 );
103
+ delay (60 );
104
+ analogWrite (BUZZER_PIN, 0 );
105
+ }else if (type.equals (" long" )){
106
+ analogWrite (BUZZER_PIN, 60 );
107
+ delay (1000 );
108
+ analogWrite (BUZZER_PIN, 0 );
109
+ } else {
110
+ analogWrite (BUZZER_PIN, 60 );
111
+ delay (60 );
112
+ analogWrite (BUZZER_PIN, 0 );
113
+ }
114
+ }
0 commit comments