-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoRFIDlock.pde
305 lines (250 loc) · 5.9 KB
/
ArduinoRFIDlock.pde
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <NewSoftSerial.h>
//#include <EEPROM.h>
#include <Servo2.h>
/*#ifndef TEST
#define TEST
#include <ArduinoTestSuite.h>
#include "TestIdStorage.h"
#endif
*/
//#include "IdStorage.h"
Servo myservo;
const int SerInToArdu=2; //Defines pin data passes to Arduino over from RFID reader
const int SerOutFrmArdu=4; //Not used, but
//"fills" a parameter in the set up of
//mySerialPort
const int buttonPin = 8; // Digital pin for button
int lastButton = HIGH;
int currentButton = HIGH;
const int ACCESS_DENIED = 0;
const int TOGGLE_DOORE_LOCK = 1;
const int TOGGLE_ADMIN_MODE = 2;
const int SERVO_PIN = 9;
const int RED_LED_PIN = 6;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
const int INTERNAL_LED = 5;
const int DOOR_BUTTON_PIN = 7;
const int RED_INTENSITY = 100;
const int GREEN_INTENSITY = 100;
boolean doorIsOpen = true;
NewSoftSerial mySerialPort(SerInToArdu,SerOutFrmArdu);
// Creates serial port for RFID reader to be attached to.
// Using pins 0 and 1 is problematic, as they are also
// connecting the development PC to the Arduino for
// programming, and for the output sent to the serial
// monitor.
byte id[12];
int bytePos = 0;
int incomingByte=0;
#define STATE_ADD_USER 0
#define STATE_DOOR_LOCK 1
byte state = STATE_DOOR_LOCK;
void setup()
{
Serial.begin(9600);//For access to serial monitor channel
Serial.println("*******");
Serial.println("READY");
delay(1000);
mySerialPort.begin(9600);
myservo.attach(SERVO_PIN);
myservo.write(90);
pinMode(buttonPin,INPUT);
// IdStorageTest test;
setCorrectLight();
};
void loop()
{
//Serial.println("start loop");
// Check RFID reader
while (mySerialPort.available() > 0) {
// read the incoming byte from the serial buffer
incomingByte = mySerialPort.read();
//Serial.println("got rfid data");
if(incomingByte != 2 && incomingByte != 3) {
Serial.print(incomingByte,BYTE);
id[bytePos] = incomingByte;
bytePos = bytePos + 1;
}
else if (incomingByte==3) {
bytePos = 0;
Serial.print('\n');
}
else if (incomingByte==2) {
bytePos = 0;
Serial.print("ID: ");
}
}
// Check lock button
currentButton = debounce(lastButton);
if (lastButton == HIGH && currentButton == LOW) {
lastButton = currentButton;
buttonLoop();
}
delay(5);
lastButton = currentButton;
// Check usb
while (Serial.available() > 0) {
switch (Serial.read()) {
case '0':
Serial.println("Access denied");
blinkLight(255, 0, 255, 300, 5);
break;
case '1':
toggleDoorLock();
break;
case '2':
setState(STATE_DOOR_LOCK);
Serial.println("Set to door lock mode");
break;
case '3':
setState(STATE_ADD_USER);
Serial.println("Set to add user mode");
break;
}
}
}
void buttonLoop(){
int ms = 0;
int cycle = 0;
int led = 255;
int blinkPeriod = 500;
analogWrite(INTERNAL_LED,led);
changeColor(led,led,0);
// Waiting for realese of button
while(isButtonPushed())
delay(20);
// Waiting for button to be pushed or door to open
while( doorIsOpen && (isDoorClosed() || isButtonPushed()) ){
delay(20);
}
while (true){
ms += 5;
cycle += 5;
delay(5);
//currentButton = debounce(lastButton);
if (isDoorClosed())//(lastButton == HIGH && currentButton == LOW){
break;
if (cycle >= blinkPeriod){
cycle = 0;
if(led == 0)
led = 255;
else
led = 0;
analogWrite(INTERNAL_LED,led);
changeColor(led,led,0);
}
}
analogWrite(INTERNAL_LED,0);
changeColor(0,0,0);
delay(500);
toggleDoorLock();
}
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin);
if (last != current) {
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
void setState(int newState){
state = newState;
setCorrectLight();
}
void toggleDoorLock() {
if (doorIsOpen) {
Serial.println("Locking door");
//myservo.write(5);
turnCW(800);
}
else {
Serial.println("Opening door");
turnCCW(800);
//myservo.write(175);
}
doorIsOpen = !doorIsOpen;
setCorrectLight();
}
void turnCCW(int waitTime) {
myservo.write(180);
redToGreen(waitTime);
myservo.write(90);
}
void turnCW(int waitTime) {
myservo.write(0);
greenToRed(waitTime);
myservo.write(90);
}
boolean isButtonPushed() {
return digitalRead(buttonPin) == LOW;
}
boolean isDoorClosed() {
return digitalRead(DOOR_BUTTON_PIN) == LOW;
}
void changeColor(int red, int green, int blue){
analogWrite(RED_LED_PIN,red);
analogWrite(GREEN_LED_PIN,green);
analogWrite(BLUE_LED_PIN,blue);
}
void setCorrectLight(){
if (state == STATE_DOOR_LOCK){
if(doorIsOpen) {
analogWrite(INTERNAL_LED,0);
changeColor(0,GREEN_INTENSITY,0);
}
else {
changeColor(RED_INTENSITY,0,RED_INTENSITY);
analogWrite(INTERNAL_LED,100);
}
}
else {
changeColor(255,255,255);
}
}
void blinkLight(int red, int green, int blue, int period, int cycles) {
for (int i = 0; i<cycles; i++) {
changeColor(red,green,blue);
delay(period/2);
changeColor(0,0,0);
delay(period/2);
}
setCorrectLight();
}
void redToGreen(int waitTime) {
int red = RED_INTENSITY;
int green = 0;
int waitedTime = 0;
for (int i = 0; i<RED_INTENSITY; i++) {
red --;
changeColor(red,green,red);
delay(2);
waitedTime = waitedTime + 2;
}
for (int i = 0; i<255; i++) {
green ++;
changeColor(red,green,red);
delay(1);
waitedTime++;
}
delay(waitTime-waitedTime);
}
void greenToRed(int waitTime) {
int red = 0;
int green = GREEN_INTENSITY;
int waitedTime = 0;
for (int i = 0; i<GREEN_INTENSITY; i++) {
green --;
changeColor(red,green,red);
delay(2);
waitedTime = waitedTime + 2;
}
for (int i = 0; i<255; i++) {
red ++;
changeColor(red,green,red);
delay(1);
waitedTime++;
}
delay(waitTime-waitedTime);
}