1#include <AnalogPin.h>
2#include <Keyboard.h>
3#include <SPI.h>
4#include
5 <Wire.h>
6#include <MFRC522.h>
7#include <SoftwareSerial.h>
8#include <Ultrasonic.h>
9#include
10 <Adafruit_SSD1306.h>
11#include <U8g2_for_Adafruit_GFX.h>
12
13#define LED 13
14
15AnalogPin
16 Apin(A0);
17U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
18MFRC522 mfrc522(SS,
19 5);
20MFRC522::MIFARE_Key key;
21SoftwareSerial EEBlue(11, 10);
22Ultrasonic
23 ultrasonic(8, 7, 68000UL);
24Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32,
25 &Wire, 13, 400000UL, 100000UL);
26
27const char card1[32] = "EB1CEFFC";
28const
29 long passwordExpireInterval = 3600000;
30char str[32] = "";
31String readid,
32 password = "";
33byte readCard[4];
34byte distance, i, lastx, x = 0;
35byte
36 isLogged = 0;
37unsigned long lastPasswordCheck, lastPotChange, start = 0;
38
39void
40 setup() {
41 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
42 u8g2_for_adafruit_gfx.begin(display);
43
44 display.clearDisplay();
45 display.display();
46 pinMode(LED, OUTPUT);
47
48 pinMode(11, INPUT);
49 pinMode(10, OUTPUT);
50 digitalWrite(LED, HIGH);
51
52 Serial.begin(57600);
53 SPI.begin();
54 Keyboard.begin();
55 EEBlue.begin(9600);
56
57 mfrc522.PCD_Init();
58}
59void setPassword() {
60 if (EEBlue.available()
61 > 0) {
62 password = EEBlue.readStringUntil('\
63');
64 }
65}
66void resetPassword()
67 {
68 if ((millis() - lastPasswordCheck) >= passwordExpireInterval) {
69 password
70 = "";
71 lastPasswordCheck = millis();
72 }
73}
74void loginCommand()
75 {
76 delay(400);
77 Keyboard.press(KEY_LEFT_CTRL);
78 Keyboard.press('a');
79
80 Keyboard.releaseAll();
81 delay(400);
82 Keyboard.print(password);
83
84 Keyboard.releaseAll();
85 delay(100);
86 Keyboard.press(KEY_RETURN);
87
88 Keyboard.release(KEY_RETURN);
89 isLogged = 1;
90 digitalWrite(LED, HIGH);
91
92 message();
93}
94void winLock() {
95 Keyboard.press(KEY_LEFT_GUI);
96 Keyboard.press('l');
97
98 Keyboard.releaseAll();
99 isLogged = 0;
100 digitalWrite(LED, LOW);
101
102 message();
103}
104void message() {
105 display.clearDisplay();
106 u8g2_for_adafruit_gfx.setFont(u8g2_font_doomalpha04_tr);
107
108 if (isLogged == 0) {
109 u8g2_for_adafruit_gfx.drawStr(0, 32, "LOCKED");
110
111 } else {
112 u8g2_for_adafruit_gfx.drawStr(0, 32, "UNLOCKED");
113 }
114
115 display.display();
116 delay(1000);
117}
118String arrayToString(byte array[],
119 unsigned int len, char buffer[]) {
120 for (unsigned int i = 0; i < len; i++)
121 {
122 byte nib1 = (array[i] >> 4) & 0x0F;
123 byte nib2 = (array[i] >>
124 0) & 0x0F;
125 buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
126
127 buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
128 }
129
130 buffer[len * 2] = '\\0';
131 return buffer;
132}
133void getID() {
134 if
135 (!mfrc522.PICC_IsNewCardPresent()) {
136 return;
137 }
138 if (!mfrc522.PICC_ReadCardSerial())
139 {
140 return;
141 }
142 for (int i = 0; i < mfrc522.uid.size; i++) {
143
144 readCard[i] = mfrc522.uid.uidByte[i];
145 }
146 mfrc522.PICC_HaltA();
147}
148void
149 deleteCardInfo() {
150 for (int i = 0; i < mfrc522.uid.size; i++) {
151 readCard[i]
152 = "";
153 }
154 readid = "";
155}
156byte readDistance() {
157 return
158 ultrasonic.read(CM);
159}
160
161void countDown() {
162 if (start == 0 || i ==
163 0) {
164 start = millis();
165 i = 11;
166 }
167 long last;
168 while
169 (i > 0 && distance > x) {
170 last = millis();
171 distance = readDistance();
172
173 Apin.setNoiseThreshold(4);
174 x = map(Apin.read(), 0, 1023, 30, 200);
175
176 if (last - start >= 1500) {
177 display.clearDisplay();
178 u8g2_for_adafruit_gfx.setFont(u8g2_font_doomalpha04_tr);
179
180 u8g2_for_adafruit_gfx.drawStr(0, 32, "LOCKING IN");
181 u8g2_for_adafruit_gfx.setCursor(70,
182 32);
183 u8g2_for_adafruit_gfx.print(i - 1);
184 (i > 10) ? u8g2_for_adafruit_gfx.drawStr(85,
185 32, "sec") : u8g2_for_adafruit_gfx.drawStr(80, 32, "sec");
186 display.display();
187
188 i--;
189 if (i == 0) {
190 delay(1000);
191 winLock();
192
193 distance = 0;
194 start = 0;
195 }
196 start
197 += 1000;
198 last = millis();
199 }
200 }
201 distance = 0;
202
203 start = 0;
204 i = 0;
205}
206void loop() {
207 Apin.setNoiseThreshold(5);
208
209 x = map(Apin.read(), 0, 1023, 30, 200);
210 distance = readDistance();
211
212 if (distance > x && isLogged == 1) {
213 countDown();
214 } else {
215
216 if (lastx == x && millis() - lastPotChange > 2000) {
217 display.clearDisplay();
218
219 u8g2_for_adafruit_gfx.setFont(u8g2_font_doomalpha04_tr);
220 u8g2_for_adafruit_gfx.drawStr(0,
221 32, "STATUS OK");
222 } else if (lastx != x) {
223 display.clearDisplay();
224
225 u8g2_for_adafruit_gfx.setFont(u8g2_font_open_iconic_embedded_2x_t);
226
227 u8g2_for_adafruit_gfx.drawGlyph(0, 32, 0x0050);
228 u8g2_for_adafruit_gfx.setFont(u8g2_font_doomalpha04_tr);
229
230 u8g2_for_adafruit_gfx.setCursor(30, 31);
231 u8g2_for_adafruit_gfx.print(x);
232
233 if (x > 99) {
234 u8g2_for_adafruit_gfx.drawStr(55, 32, "CM");
235
236 } else {
237 u8g2_for_adafruit_gfx.drawStr(50, 32, "CM");
238
239 }
240 lastx = x;
241 lastPotChange = millis();
242 }
243
244 display.display();
245 }
246 getID();
247 readid = arrayToString(readCard,
248 4, str);
249 setPassword();
250 resetPassword();
251 if (password != ""
252 && password.length() > 2 && readid == card1 && isLogged == 0) {
253 loginCommand();
254
255 } else if (readid == card1 && isLogged == 1) {
256 winLock();
257 }
258
259 deleteCardInfo();
260}
261