1 | /*
|
2 | Made by Marco Fusco
|
3 | May 20, 2016
|
4 | Contact me at: marcofusco111@gmail.com
|
5 | */
|
6 |
|
7 |
|
8 | #include <LiquidCrystal.h>
|
9 | #include <EEPROM.h>
|
10 | #include <Servo.h>
|
11 |
|
12 | int address = 0;
|
13 | static unsigned long SaveTimer;
|
14 | static unsigned long SaveDelay = (30 * 1000);
|
15 |
|
16 |
|
17 | char CODE[10] = "1234E";
|
18 | char Str[10];
|
19 | char CodeLength = 4;
|
20 | int Pos = 0;
|
21 | bool Unlocked;
|
22 | static unsigned long DisplayTimer;
|
23 | static unsigned long DisplayDelay = 200;
|
24 |
|
25 | LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
|
26 |
|
27 | int relaisPin = A0; //+++
|
28 |
|
29 | int buttonPin1 = 2;
|
30 | int buttonPin2 = 3;
|
31 | int buttonPin3 = 4;
|
32 | int buttonPin4 = 5;
|
33 |
|
34 | int enterbutton = 10;
|
35 | int clearlockbutton = 13;
|
36 |
|
37 | //Servo myServo;
|
38 |
|
39 | void setup() {
|
40 |
|
41 | //myServo.attach(A1);
|
42 |
|
43 | int EEPROMCodeOK = true;
|
44 | for (Pos = 0; Pos <= (CodeLength); Pos++) {
|
45 | Str[Pos] = EEPROM.read(Pos);
|
46 | if (!(strrchr("1123456789", Str[Pos]))) {
|
47 | // not a valid code
|
48 | EEPROMCodeOK = false;
|
49 | }
|
50 | }
|
51 | Pos++;
|
52 | Str[Pos] = EEPROM.read(Pos);
|
53 | if (Str[CodeLength + 1] != 'E') EEPROMCodeOK = false;
|
54 | if (EEPROMCodeOK) {
|
55 | Str[CodeLength + 2] = '\0';
|
56 | strncpy(CODE, Str, CodeLength + 1);
|
57 | }
|
58 | ClearCode();
|
59 |
|
60 | pinMode(relaisPin, OUTPUT) //+++
|
61 | pinMode(buttonPin1, INPUT_PULLUP);
|
62 | pinMode(buttonPin2, INPUT_PULLUP);
|
63 | pinMode(buttonPin3, INPUT_PULLUP);
|
64 | pinMode(buttonPin4, INPUT_PULLUP);
|
65 |
|
66 | pinMode(enterbutton, INPUT_PULLUP);
|
67 | pinMode(clearlockbutton, INPUT_PULLUP);
|
68 |
|
69 | lcd.begin(16, 2);
|
70 | lcd.setCursor(0, 0);
|
71 | lcd.print("Hello.");
|
72 | delay(2000);
|
73 | lcd.clear();
|
74 | lcd.setCursor(0, 0);
|
75 | lcd.print("Password:");
|
76 |
|
77 | DisplayTimer = millis() + 200;
|
78 | }
|
79 |
|
80 |
|
81 | void loop() {
|
82 |
|
83 | Lock();
|
84 |
|
85 | Pos = constrain(Pos, 0, CodeLength);
|
86 |
|
87 | int buttonState1 = digitalRead(buttonPin1);
|
88 | int buttonState2 = digitalRead(buttonPin2);
|
89 | int buttonState3 = digitalRead(buttonPin3);
|
90 | int buttonState4 = digitalRead(buttonPin4);
|
91 |
|
92 | int clButtonState = digitalRead(clearlockbutton);
|
93 | int enterButtonState = digitalRead(enterbutton);
|
94 |
|
95 | lcd.setCursor(9, 0);
|
96 |
|
97 | if (buttonState1 == LOW) {
|
98 | Str[Pos] = '1';
|
99 | Pos++;
|
100 | Str[Pos] = '\0';
|
101 | delay(250);
|
102 | while (digitalRead(buttonPin1) == LOW);
|
103 |
|
104 | }
|
105 |
|
106 | else if (buttonState2 == LOW) {
|
107 | Str[Pos] = '2';
|
108 | Pos++;
|
109 | Str[Pos] = '\0';
|
110 | delay(250);
|
111 | while (digitalRead(buttonPin2) == LOW);
|
112 |
|
113 | }
|
114 |
|
115 | else if (buttonState3 == LOW) {
|
116 | Str[Pos] = '3';
|
117 | Pos++;
|
118 | Str[Pos] = '\0';
|
119 | delay(250);
|
120 | while (digitalRead(buttonPin3) == LOW);
|
121 | }
|
122 |
|
123 | else if (buttonState4 == LOW) {
|
124 | Str[Pos] = '4';
|
125 | Pos++;
|
126 | Str[Pos] = '\0';
|
127 | delay(250);
|
128 | while (digitalRead(buttonPin4) == LOW);
|
129 |
|
130 | }
|
131 | else if (enterButtonState == LOW) {
|
132 | Str[Pos] = 'E';
|
133 | Pos++;
|
134 | Str[Pos] = '\0';
|
135 | delay(250);
|
136 | while (digitalRead(buttonPin1) == LOW);
|
137 | if (strcmp (Str,CODE) == 0) {
|
138 | Unlocked = true;
|
139 | lcd.setCursor(0, 0);
|
140 | lcd.print(" Access Granted");
|
141 | delay(2000);
|
142 | lcd.clear();
|
143 | lcd.print(" Unlocked");
|
144 | }
|
145 | else if (SaveTimer > millis() && (Pos + 1) == CodeLength) {
|
146 |
|
147 | strcpy(CODE, Str);
|
148 | for (Pos = 0; Pos <= (CodeLength + 1); Pos++) {
|
149 | EEPROM.write(Pos, Str[Pos]);
|
150 | }
|
151 | lcd.setCursor(0, 0);
|
152 | lcd.print("Saving Code:");
|
153 | lcd.setCursor(0, 1);
|
154 | lcd.print(Str);
|
155 |
|
156 | Unlocked = true;
|
157 | }
|
158 |
|
159 | else {
|
160 |
|
161 | lcd.clear();
|
162 | lcd.print(" Access Denied.");
|
163 | delay(2000);
|
164 | lcd.clear();
|
165 | lcd.print("Password:");
|
166 |
|
167 | }
|
168 |
|
169 | while (Unlocked) {
|
170 | Unlock();
|
171 | if (digitalRead(clearlockbutton) == LOW) {
|
172 | delay(200);
|
173 | lcd.clear();
|
174 | lcd.print(" Locked");
|
175 | delay(2000);
|
176 | lcd.clear();
|
177 | Unlocked = false;
|
178 | SaveTimer = millis() + 30000;
|
179 | }
|
180 | }
|
181 |
|
182 | ClearCode();
|
183 |
|
184 |
|
185 | }
|
186 |
|
187 | else if (clButtonState == LOW) {
|
188 | delay(500);
|
189 |
|
190 | while (clearlockbutton == LOW);
|
191 | if ((millis() - SaveTimer) > 4500) {
|
192 |
|
193 | }
|
194 |
|
195 | ClearCode();
|
196 |
|
197 | }
|
198 |
|
199 | if ( (long)( millis() - DisplayTimer ) >= 0) {
|
200 | DisplayTimer += DisplayDelay;
|
201 | lcd.setCursor(9, 0);
|
202 | lcd.print(Str);
|
203 | lcd.print(" ");
|
204 |
|
205 | }
|
206 | }
|
207 |
|
208 | void ClearCode() {
|
209 |
|
210 | Pos = 0;
|
211 | Str[Pos] = '\0';
|
212 | lcd.setCursor(0, 0);
|
213 | lcd.print("Password:");
|
214 | lcd.setCursor(0, 1);
|
215 | lcd.print(" ");
|
216 |
|
217 | }
|
218 |
|
219 | void Unlock() {
|
220 | digitalWrite(relaisPin, HIGH) //+++
|
221 |
|
222 | //myServo.write(150);
|
223 |
|
224 | }
|
225 |
|
226 | void Lock() {
|
227 | digitalWrite(relaisPin, LOW) //+++
|
228 |
|
229 | //myServo.write(50);
|
230 |
|
231 | }
|