-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_design_14.ino
249 lines (169 loc) · 5.15 KB
/
lock_design_14.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
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
//need to include the servo library
#include <Servo.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
//These are the pins variables4
#define Redpin 6
#define analogPin A0
#define Greenpin 5
const int buttonPin = 19;
//const int servoPin = 18;
const int magnetPin =12;
const int sensorClosed = 21;
const int RFIDpin = 23;
//These are the other variables
int startTime, buttonPosition, RFIDcheck, closedCheck,RFIDstartTime,open1,closed,voltageIn,magnetlockstate;
bool opencheck;
//This is the servo
//Servo myservo;
void setup()
{
//Variables not pins
closed = 1;
open1 = 0;
RFIDcheck = 0;
// RFIDbadcheck = 0;
buttonPosition =0;
closedCheck = 0;
//servoPosition = 0;
magnetlockstate = 0;
opencheck = true;
digitalWrite(magnetPin,LOW);
// myservo.write(0); // Represents a closed lock
// myservo.attach(servoPin);
pinMode(Redpin,OUTPUT);
pinMode(Greenpin,OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(RFIDpin, INPUT);
}
void loop()
{
//Here we need to check battery level, the only way to do this is to measure the analog c
///We need a voltage divider for this part. Max input to ADC is 1 V
voltageIn = analogRead(analogPin)/1023;
if(voltageIn<.8){
//Battery is at 20% (2.66Volts)
}
else if(voltageIn<.87){
//Batttery is at 50% capacity (2.9 Volts)
}
//servoPosition = myservo.read();
magnetlockstate = digitalRead(magnetPin);
if(digitalRead(buttonPin)==HIGH){
buttonPosition =1;
startTime = millis();
}
if(!(opencheck)&&(buttonPosition == 1)){
if((millis()-startTime)<4000){
//This is if someone wants to scan a card
// If the Scan is good
RFIDcheck = RFIDscan(RFIDpin,RFIDcheck,0,250,0,0);
// If the Scan is bad
if((RFIDcheck ==0)&&(magnetlockstate==open1)){
buttonPosition = 0;
opencheck = true;
}
}
buttonPosition = 0;
}
// original is if digitalRead(sensorClosed) == HIGH)
if((true)&&(opencheck)){
if(buttonPosition ==1){ //This is if somone presses the lock button
lockButton(buttonPosition);
buttonPosition = 0;
opencheck = false;
// goToSleep();
//This is where we need to set the arduinon to sleep again
}
// original is if digitalRead(sensorClosed) == HIGH)
else if((true)&&(closedCheck==0)){ //This is if someone forgets to close
if(magnetlockstate != closed){
closedCheck=1;
startTime = millis();
}
}
if(closedCheck==1)
{
if((millis()-startTime) >= 1000)
{
setRGB(250,0);
//myservo.write(closed);
digitalWrite(magnetPin,HIGH);
buttonPosition = 0;
//This is where we need to set the arduino to sleep again
closedCheck =0;
opencheck = false;
//goToSleep();
}
}
}
}
/*
void goToSleep(){
ESP.deepSleep(0);
}
*/
void lockButton(int buttonPosition)
{
//locks together
if(buttonPosition == 1){
setRGB(250,0);
// myservo.write(closed);
digitalWrite(magnetPin,HIGH);
}
if(magnetlockstate == closed){
setRGB(100,0);
buttonPosition =0;
}
}
//_______________________________________________\\
///// RFID SCAN FUNCTION \\\\
//This encompases the RFID scan to be general for both scans which means less code
int RFIDscan(int RFIDpin,int RFIDcheck, int red,int green, int red2, int magnetpos){
if((getScanResults(RFIDpin))&&(RFIDcheck==0)){
RFIDcheck=1;
RFIDstartTime = millis();
}else if(!(getScanResults(RFIDpin))&&(RFIDcheck==0)){
red = 250;
green = 0;
red2 = 250;
magnetpos = closed;
RFIDcheck=1;
}
if(RFIDcheck==1){
setRGB(red,green);
//myservo.write(servopos);
digitalWrite(magnetPin,magnetpos);
if((millis()-RFIDstartTime)>=500)
{
setRGB(red2,0);
RFIDcheck = 0;
}
}
return RFIDcheck;
}
/////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//_______________________________________________\\
//// This sets the rgb to its distinct colours \\\\
// Red - Bad Scan / Locked
// Green - Good Scan
// Blue - not used
void setRGB(int red,int green)
{
analogWrite(Redpin,red);
analogWrite(Greenpin,green);
}
////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\
//_______________________________________________\\
/// This checks the state of a given button \\\\\\
// In Final Product it will be replaced with checking
// whether a user is recognized in the system.
/////The digitalRead is to be replaced with the RDID Scan code
bool getScanResults(int RFID){
if(digitalRead(RFID)==HIGH){///Here is where we check wether the user is recognized
return true; /// If the user is recognized
}
else if(digitalRead(RFID)==LOW){
return false; /// If the user is not recognized
}
}