1#include <Wire.h>
2#include <Servo.h>
3#include <Keypad.h>
4#include <U8x8lib.h>
5
6#define Password_Length 7
7
8U8X8_SSD1306_128X64_NONAME_HW_I2C oleg;
9int signalPin = 12;
10
11char Data[Password_Length];
12char Master[Password_Length] = "160306";
13byte data_count = 0, master_count = 0;
14bool Pass_is_good;
15char customKey;
16float voltage1 = 5;
17const byte ROWS = 4;
18const byte COLS = 3;
19int pos = 0;
20const int a = 1;
21const int b = 0;
22const int ledPin = 10;
23const int closePin =11;
24
25char hexaKeys[ROWS][COLS] = {
26 {'1', '2', '3'},
27 {'4', '5', '6'},
28 {'7', '8', '9'},
29 {'*', '0', '#'}
30};
31
32byte rowPins[ROWS] = {2, 3, 4, 5};
33byte colPins[COLS] = {6, 7, 8};
34
35Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
36Servo lock;
37Servo unlock;
38void setup() {
39 Serial.begin(9600);
40 oleg.begin();
41 oleg.setFont(u8x8_font_profont29_2x3_r);
42 pinMode(ledPin, OUTPUT);
43 pinMode(closePin, OUTPUT);
44
45 Wire.begin();
46 lock.attach(13);
47 unlock.attach(9);
48
49 digitalWrite(closePin, HIGH);
50}
51
52void clearData() {
53 while (data_count != 0) {
54 Data[data_count--] = 0;
55 }
56 return;
57}
58
59void ledFlash() {
60 digitalWrite(ledPin, LOW);
61 delay(250);
62 digitalWrite(ledPin, HIGH);
63 delay(250);
64 digitalWrite(ledPin, LOW);
65 delay(250);
66 digitalWrite(ledPin, HIGH);
67 delay(250);
68 digitalWrite(ledPin, LOW);
69 delay(250);
70 digitalWrite(ledPin, HIGH);
71 delay(250);
72}
73void closeFlash() {
74 digitalWrite(closePin, HIGH);
75 delay(250);
76 digitalWrite(closePin, LOW);
77 delay(250);
78 digitalWrite(closePin, HIGH);
79 delay(250);
80 digitalWrite(closePin, LOW);
81 delay(250);
82 digitalWrite(closePin, HIGH);
83 delay(250);
84 digitalWrite(closePin, LOW);
85 delay(250);
86 digitalWrite(closePin, HIGH);
87 delay(250);
88 digitalWrite(closePin, LOW);
89 delay(250);
90 digitalWrite(closePin, HIGH);
91 delay(250);
92}
93void loop() {
94 static byte theState = 1;
95
96
97 oleg.setCursor(5, 0);
98 oleg.print("Enter");
99 oleg.setCursor(0, 3);
100 oleg.print("password");
101 oleg.setCursor(0, 0);
102 lock.write(100);
103
104 customKey = customKeypad.getKey();
105 if (customKey) {
106 oleg.print(" ");
107 oleg.setCursor(0, 3);
108 oleg.print(" ");
109 Data[data_count] = customKey;
110 oleg.setCursor(data_count, 1);
111 oleg.print(Data[data_count]);
112 data_count++;
113 delay(500);
114 }
115
116 if (data_count == Password_Length - 1) {
117 oleg.print(" ");
118 oleg.setCursor(0, 3);
119 oleg.print(" ");
120 oleg.setCursor(0, 0);
121 if (!strcmp(Data, Master)) {
122 oleg.print("Correct");
123 digitalWrite(ledPin, HIGH);
124 digitalWrite(closePin, LOW);
125 lock.write(0);
126 delay(30000);
127 oleg.setCursor(0, 0);
128 oleg.print(" ");
129 oleg.setCursor(0, 3);
130 oleg.print(" ");
131 oleg.setCursor(0, 0);
132 oleg.print("closing");
133 ledFlash();
134 digitalWrite(ledPin, LOW);
135 closeFlash();
136 digitalWrite(closePin, HIGH);
137 lock.write(100);
138 oleg.setCursor(0, 0);
139 oleg.print(" ");
140 oleg.setCursor(0, 3);
141 oleg.print(" ");
142 } else {
143 oleg.print("Incorrect");
144 delay(1000);
145 oleg.setCursor(0, 0);
146 oleg.print(" ");
147 oleg.setCursor(0, 3);
148 oleg.print(" ");
149 lock.write(100);
150 }
151
152
153 clearData();
154 }
155}
156