-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFID.ino
127 lines (109 loc) · 2.58 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN , RST_PIN);
int pin1 = 3;
int pin2 = 4;
bool CarDoor = 0;
int O;
int c;
void setup() {
Serial.begin(115200);
Serial.println("Approximate your card to the reader...");
SPI.begin();
mfrc522.PCD_Init();
Serial.println();
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
}
void loop() {
//c=digitalRead(6);
//O=digitalRead(5);
//if(O==0&&CarDoor==0)
//{
// CarDoor=1;
// Serial.println("open");
// digitalWrite(pin1,HIGH);
// digitalWrite(pin2,LOW);
// delay (5000);
// digitalWrite(pin1,LOW);
// digitalWrite(pin2,LOW);
//
//}
//if(c==0&&CarDoor==1){
// CarDoor=0;
// Serial.println("close");
// digitalWrite(pin1,LOW);
// digitalWrite(pin2,HIGH);
// delay (5000);
// digitalWrite(pin1,LOW);
// digitalWrite(pin2,LOW);
//
//}
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print("UID tag:");
String content = "";
byte letter;
for (byte i = 0 ; i < mfrc522.uid.size ; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0X10 ? "0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0X10 ? "0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "16 1F 15 7E" && CarDoor == 0)
{
Serial.println("Authorized access");
Serial.println();
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay (5000);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
CarDoor = 1;
}
else {
Serial.println("Access denied");
delay(3000);
}
}
void serialEvent() {
String OpenDoorMsg = "IOPC";
String CloseDoorMsg = "ICLC";
while (Serial.available()) {
String incomingByte = Serial.readString();
if ( incomingByte.startsWith(OpenDoorMsg) ) {
if (CarDoor == 0)
{
CarDoor = 1;
Serial.println("open");
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay (5000);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}
else if (incomingByte.startsWith(CloseDoorMsg)) {
if (CarDoor == 1) {
CarDoor = 0;
Serial.println("close");
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delay (5000);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}
}
}