-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoorLock.ino
131 lines (103 loc) · 2.89 KB
/
DoorLock.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
#include "Keypad.h"
int motor = 2;
int redLed = 4;
int greenLed = 3;
int buttonLock = 13;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6, 5}; //connect to the column pinouts of the keypad
Keypad customKeypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int STATEA = 0;
int STATEB = 0;
char passA[4] = {'A','B','C','D'};
char passB[4] = {'1','2','3','4'};
unsigned long previousMillis = 0;
const long interval = 10000;
bool keyPressed(char customKey);
void feedActivityLED(unsigned long currentMillis);
void unlockProc();
void setup() {
// put your setup code here, to run once:
pinMode(motor, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonLock, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(motor, LOW); //LOCK
unsigned long currentMillis = millis();
feedActivityLED(currentMillis);
char customKey = customKeypad.getKey();
if(keyPressed(customKey)){
previousMillis = currentMillis;
feedActivityLED(currentMillis);
digitalWrite(greenLed, HIGH);
Serial.print(">>");
Serial.println(customKey);
if(customKey==passA[STATEA]){
STATEA++;
Serial.println(customKey);
}else{
STATEA=0;
}
if(digitalRead(buttonLock)== HIGH){
if(customKey==passB[STATEB]){
STATEB++;
Serial.println(customKey);
}else{
STATEB=0;
}
}else{
digitalWrite(greenLed, HIGH);
delay(100);
digitalWrite(greenLed, LOW);
delay(100);
digitalWrite(greenLed, HIGH);
delay(100);
digitalWrite(greenLed, LOW);
}
delay(250);
digitalWrite(greenLed, LOW);
if(STATEA==4){
STATEA=0;
STATEB=0;
unlockProc();
}
if(STATEB==4){
STATEA=0;
STATEB=0;
//BUZZ IN
unlockProc();
}
}
}
bool keyPressed(char customKey){
return ((customKey>='0' && customKey<='9') || customKey=='*' || customKey=='#' || (customKey>='A' && customKey<='D'));
}
void feedActivityLED(unsigned long currentMillis){
//To save power i only set HIGH on red led in case of recent activity
bool keyPressedRecently = currentMillis - previousMillis <= interval;
if(keyPressedRecently){
digitalWrite(redLed, HIGH);
}else{
digitalWrite(redLed, LOW);
}
}
void unlockProc(){
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(motor, HIGH); //UNLOCK
Serial.println("open");
delay(1500); //wait 1,5 seconds
digitalWrite(greenLed, LOW);
digitalWrite(motor, LOW); //LOCK
Serial.println("closed");
}