-
Notifications
You must be signed in to change notification settings - Fork 0
/
Door.h
159 lines (116 loc) · 2.46 KB
/
Door.h
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
#include <Servo.h>
#include "Timer.h"
#ifndef _DOOR_H
#define _DOOR_H
class Door {
int _doorPin;
int _gl1Pin;
int _gl2Pin;
int _yl1Pin;
int _yl2Pin;
int ylOn = 0;
Servo myservo;
int pos = 0;
Timer timer;
Timer timerLight;
enum State {
OPEN,
CLOSED,
};
State _currentState = CLOSED;
State _targetState = CLOSED;
public:
Door(int doorPin, int gl1Pin, int gl2Pin, int yl1Pin, int yl2Pin){
_doorPin = doorPin;
_gl1Pin = gl1Pin;
_gl2Pin = gl2Pin;
_yl1Pin = yl1Pin;
_yl2Pin = yl2Pin;
pinMode(_gl1Pin, OUTPUT);
pinMode(_gl2Pin, OUTPUT);
pinMode(_yl1Pin, OUTPUT);
pinMode(_yl2Pin, OUTPUT);
}
void setup(){
myservo.attach(_doorPin);
myservo.write(0);
}
bool doorIsClosed(){
if (_currentState == CLOSED){
return true;
} else {
return false;
}
}
void open() {
_targetState = OPEN;
}
void close() {
_targetState = CLOSED;
}
void loop(){
if (_targetState == OPEN) {
openDoor();
} else {
closeDoor();
}
}
private:
void openDoor(){
if (timer.isTimerReady() && pos <= 180 && _currentState != OPEN){
myservo.write(pos);
pos +=1;
timer.startTimer(15);
if (timerLight.isTimerReady()){
yellowLight();
timerLight.startTimer(300);
}
}
if (myservo.read() == 180){
pos = 180;
digitalWrite(_yl1Pin, LOW);
digitalWrite(_yl2Pin, LOW);
_currentState = OPEN;
}
greenLight();
}
void closeDoor(){
if (timer.isTimerReady() && pos >= 0 && _currentState != CLOSED){
myservo.write(pos);
pos -=1;
timer.startTimer(15);
if (timerLight.isTimerReady()){
yellowLight();
timerLight.startTimer(300);
}
}
if (myservo.read() == 0){
pos = 0;
digitalWrite(_yl1Pin, LOW);
digitalWrite(_yl2Pin, LOW);
_currentState = CLOSED;
}
greenLight();
}
void greenLight(){
if (myservo.read() == 180){
digitalWrite(_gl1Pin, HIGH);
digitalWrite(_gl2Pin, HIGH);
} else {
digitalWrite(_gl1Pin, LOW);
digitalWrite(_gl2Pin, LOW);
}
}
void yellowLight(){
if (ylOn == 0){
digitalWrite(_yl1Pin, HIGH);
digitalWrite(_yl2Pin, HIGH);
ylOn = 1;
} else {
digitalWrite(_yl1Pin, LOW);
digitalWrite(_yl2Pin, LOW);
ylOn = 0;
}
}
};
#endif // _DOOR_H