-
Notifications
You must be signed in to change notification settings - Fork 0
/
forgefinal.ino
204 lines (178 loc) · 4.67 KB
/
forgefinal.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
int buttonstate, startTime, buttonchange,startTimeRFID;
int goodscan, badscan, noscan,scancheck;
int truecheck;
int redblinktime;
int noscanblinktime;
int isopen,isclosed;
bool hasBeenOpened = false;
/////////////////////////////
#define SS_PIN 21 //D2
#define RST_PIN 22 //D1
#define buttonPin 33
#define magnetPin 25
#define redPin 27
#define greenPin 26
#include <SPI.h>
#include <MFRC522.h>
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int statuss = 0;
int out = 0;
//////////////////////////////
void setup() {
buttonchange = 0;
Serial.begin(9600);
truecheck = 0;
isopen = 650;
isclosed = 25;
scancheck = 0;
buttonstate = 0;
goodscan = 2;
badscan = 1;
noscan = 0;
////////////////////////////////////////
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init();
////////////////////////////////////////
pinMode(buttonPin,INPUT);
pinMode(magnetPin,OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
}
void loop() {
if ((buttonstate == 0) && (digitalRead(33)==HIGH)) {
buttonchange = 1;
Serial.println("button was pressed");
startTimeRFID = millis();
startTime = millis();
truecheck = 0;
Serial.println("button first time");
}
if ((buttonchange == 1) && (digitalRead(33)==LOW)) {
buttonstate = 1;
buttonchange = 0;
Serial.println("button was released");
}
if (buttonstate == 1) {
Serial.println("button state = 1");
if ((hasBeenOpened == false)) {
if ((millis() - startTime) < 2000) {
Serial.println("begining of scan");
if (RFIDScan(startTimeRFID) == true) {
Serial.println("a scan");
buttonstate = 0;
if (truecheck == 1) {
hasBeenOpened = true;
}
}
}
if (buttonstate == 1) {
noscanblinktime = millis();
digitalWrite(magnetPin,HIGH);
ledcontrol(0,0);
while((millis()-noscanblinktime)<500){
Serial.println("no scan wait");
}
ledcontrol(0,1);
//go to sleep
hasBeenOpened = false;
buttonstate = 0;
}
} else if ((hasBeenOpened == true)) {
Serial.println("button lock");
ledcontrol(0, 1);
digitalWrite(magnetPin, 1);
buttonstate = 0;
hasBeenOpened = false;
}
}
}
bool RFIDScan(int startTimeRFID) {
int scanresults = getScanResults(startTimeRFID);
if (scanresults == goodscan) {
Serial.println("good scan");
ledcontrol(1, 0); //green on, red off
digitalWrite(magnetPin, LOW);
truecheck =1;
return true;
}
else if (scanresults == badscan) {
Serial.println("bad scan");
redblinktime = millis();
digitalWrite(magnetPin, HIGH);
ledcontrol(0,0);
while((millis()-redblinktime)<500){
Serial.println("red wair");
}
ledcontrol(0,1); //green on, red off
truecheck = 0;
return true;
}
else {
Serial.println("no scan");
return false;
}
}
void ledcontrol(int green, int red) {
Serial.println("led is changed");
if(green ==1){
digitalWrite(greenPin, HIGH);
Serial.println("Green on");
}
if(green ==0){
digitalWrite(greenPin, LOW);
}
if( red ==1){
digitalWrite(redPin, HIGH);
}
if( red ==0){
digitalWrite(redPin, LOW);
}
}
int getScanResults(int startTimeRFID) {
// Look for new cards
while((millis()-startTimeRFID)< 3000){
if ( mfrc522.PICC_IsNewCardPresent())
{
scancheck =1;
break;
}
}
if(scancheck ==1){
scancheck = 0;
}
else{
return noscan;
}
while( true)
{
if(! mfrc522.PICC_ReadCardSerial()){
break;
}
}
// Select one of the cards
//Show UID on serial monitor
Serial.println();
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));
}
content.toUpperCase();
Serial.print(" UID tag :" + content.substring(1));
Serial.println();
if (content.substring(1) == "C7 6D FC D8") //change UID of the card that you want to give access
{
//this is where instead of checking the value we need to check if the log in works
statuss = 1;
return goodscan;
}
else {
Serial.println(" Access Denied ");
return badscan;
}
}