-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ino
253 lines (187 loc) · 5.76 KB
/
code.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// This #include statement was automatically added by the Particle IDE.
#include "GarageHardware.h"
// RED indicates that the garage door is OPENED
// GREEN indicates that the garage door is CLOSED
// BLUE indicates that the garage door is in FAULT
// PURPLE indicates that the garage door is MOVING
// D7 LIGHT indicates the garage door LIGHT
enum ButtonStates {idle, pressed, held};
ButtonStates buttonState = idle;
boolean temp = false;
boolean buttonRelease = false;
String status = "";
const unsigned long MIN_BUTTON_PRESS_TIME = 150;
unsigned long previousTime = 0;
unsigned long faultTime = 0;
unsigned long previousLight = 0;
unsigned long lightTime = 3000;
unsigned long autoClose = 0;
void setup() {
setupHardware();
Serial.begin(9600);
initialState();
//Particle.function("dTimer", dTimer);
Particle.function("afterTimer", toClosingAfterTimer);
Particle.function("toClosing", toClosing);
Particle.function("toClosed", toClosed);
Particle.function("toOpened", toOpened);
Particle.function("toOpening", toOpening);
Particle.function("writeStatus", writeStatus);
Particle.function("toFault", toFault);
Particle.subscribe("garagedoor/button", remoteButtonCheck);
}
boolean remoteButton = false;
bool remoteButtonCheck(String eventName, String data) {
if(data == "pressed"){
remoteButton = true;
}
}
void initialState() {
if(isDoorFullyClosed() == true) {
status = "closed";
} else if(isDoorFullyOpen() == true) {
status = "opened";
} else {
status = "faultOpen";
}
}
int writeStatus(String x) {
if(status == "closed") {
Particle.publish("garageClosed", "garageClosed");
} else if(status == "opened") {
Particle.publish("garageOpened", "garageOpened");
} else if(status == "faultOpen") {
Particle.publish("faultOpen", "faultOpen");
}
}
int toClosingAfterTimer(String data){
int val = data.toInt();
int closeTiming = 1000*val;
autoClose = millis();
while(millis() - autoClose < closeTiming) {
stopMotorIdle();
}
motorIsClosing();
}
int toClosing(String x) {
motorIsClosing();
}
int toClosed(String x){
closeFully();
}
int toOpening(String x){
motorIsOpening();
}
int toOpened(String x){
openFully();
}
int toFault(String x){
stopMotorIdle();
}
void loop() {
//below are the functions that change the status of the door (ie. changes when button pressed..... these should
//be forced to be called by the js inorder to change the door status)
if(status == "faultClose" && (isButtonHeld() == true || remoteButton == true)) { // in fault state
motorIsOpening();
remoteButton = false;
}
else if (status == "faultOpen" && (isButtonHeld() == true || remoteButton == true)) { // in fault state
motorIsClosing();
remoteButton = false;
}
if(status == "closed" && (isButtonHeld() == true || remoteButton == true)) { // in fault state
motorIsOpening();
remoteButton = false;
}
else if (status == "opened" && (isButtonHeld() == true || remoteButton == true)) { // in fault state
motorIsClosing();
remoteButton = false;
}
else if(status =="opening" && (isFaultActive()== true || (isButtonHeld() == true || remoteButton == true))){ // goes into fault state
stopMotorIdle();
remoteButton = false;
}
else if(status =="closing" && (isFaultActive()== true || (isButtonHeld() == true || remoteButton == true))){ // goes into fault state
stopMotorIdle();
remoteButton = false;
}
else if(status == "opening" && isDoorFullyOpen() == true){ // open the door fully
openFully();
}
else if(status == "closing" && isDoorFullyClosed() == true){ // closes the door fully
closeFully();
}
}
//THESE ARE OUR OWN FUNCTIONS
boolean isButtonHeld() {
switch(buttonState) {
case idle:
if(isButtonPressed() == true) {
previousTime = millis();
temp = true;
buttonState = pressed;
}
break;
case pressed:
if(isButtonPressed() != true) {
buttonRelease = true;
buttonState = idle;
break;
}
if(((millis() - previousTime) > MIN_BUTTON_PRESS_TIME) && isButtonPressed() == true && temp == true) {
temp = false;
buttonRelease = false;
faultTime = millis();
return true;
}
break;
}
return false;
}
void openFully() {
Particle.publish("garageOpened", "garageOpened");
status = "opened";
stopMotor();
previousLight = millis();
while(millis() - previousLight < lightTime) {
setLight(true);
}
setLight(false);
}
void motorIsOpening() {
status = "opening";
Particle.publish("motorOpening", "motorOpening");
setLight(true);
startMotorOpening();
}
void closeFully() {
Particle.publish("garageClosed", "garageClosed");
status = "closed";
stopMotor();
previousLight = millis();
while(millis() - previousLight < lightTime) {
setLight(true);
}
setLight(false);
}
void motorIsClosing() {
status = "closing";
Particle.publish("motorClosing", "motorClosing");
setLight(true);
startMotorClosing();
}
void stopMotorIdle() {
stopMotor();
previousLight = millis();
while(millis() - previousLight < lightTime) {
setLight(true);
}
setLight(false);
if(status == "opening"){
status = "faultOpen";
Particle.publish("faultOpen", "faultOpen");
}
else if(status == "closing"){
status = "faultClose";
Particle.publish("faultClose", "faultClose");
}