-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoor_arduino.ino
469 lines (415 loc) · 16.3 KB
/
door_arduino.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> // https://github.com/cstim/RBD_Light
// The Bounce2 library https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#include <BounceAnalog.h> // a variation of Bounce2 with analog input
// for disabling the serial monitor below
#include <wiring_private.h>
/*
Garage-Door State machine 2
Christian Stimming, 2016, Hamburg, Germany
*/
// Names for the output pins:
const int pinOutMotorOn = 7;
const int pinOutMotorUp = 6;
const int pinOutWarnLight = 5;
const int pinOutRoomLight = 4;
const int pinOutArduinoLedPin = 13;
const int pinInButton = 3;
const int pinInButtonDown = 2;
const int pinInLightswitch = 0;
const int pinAnalogInOutsideButton = 0;
// Active this for the numerical values during debugging
//#define DEBUG
// Active this for the serial output to be compiled in.
//#define DEBUGOUTPUT
// /////////////////////////////
/// A management class for a recorded timestamp that should not be older than
/// some constraint
class ConstrainedTimestamp {
unsigned long m_value;
const unsigned long m_maxAge;
public:
ConstrainedTimestamp(unsigned long maxAge)
: m_value(0)
, m_maxAge(maxAge)
{}
void setValue(unsigned long value)
{
const unsigned long now = millis();
const unsigned long oldestValue =
(now > m_maxAge) ? (now - m_maxAge) : 0;
if (value >= now) {
m_value = now;
} else if (value <= oldestValue) {
m_value = oldestValue;
} else {
m_value = value;
}
}
unsigned long getValue() const { return m_value; }
};
// The relays are active-low, so we better define readable names for this
#define RELAY_ON LOW
#define RELAY_OFF HIGH
/** The states that the door can have*/
enum State {
DOOR_DOWN
, DOOR_UP
, DOOR_MOVING_UP
, DOOR_MOVING_DOWN
, DOOR_MOVING_DOWN_PAUSED
};
/** This is the door's actual state */
State state = DOOR_DOWN;
const unsigned long c_moveDurationTotal =
#ifdef DEBUG
5000L;
#else
17000L; // total movement takes this milliseconds; DEBUGOUTPUT: 2000
#endif
ConstrainedTimestamp g_lastMoveStart(c_moveDurationTotal);
unsigned long g_lastMovePartCompleted = 0;
const unsigned long c_moveTurnaroundPause = 300; // extra waiting time when switching from one direction to the other
const unsigned long c_movingDownPause = 800; // If the lightswitch was blocked, wait for this time
const unsigned long c_waitingTimeBeforeReclose =
#ifdef DEBUG
10*1000L;
#else
// Don't forget the trailing "L"!!!
600*1000L; // 10 minutes (600 seconds) before reclose during daylight
#endif
const unsigned long c_waitingTimeBeforeReallyReclose =
#ifdef DEBUG
5*1000L;
#else
10*1000L; // 10 seconds of warning
#endif
static inline unsigned int currentMoveCompletedToPercent() {
return 100L * g_lastMovePartCompleted / c_moveDurationTotal;
}
// The wrappers for the input buttons debouncing
Bounce inButtonDebounce;
Bounce inButtonDownDebounce;
BounceAnalog inButtonOutsideDebounce;
const unsigned long c_debounceDelay = 40;
unsigned long g_lastLightswitchBlocked = 0;
// The wrapper for the warning light output
RBD::Light outWarnLightTimer;
RBD::Light outRoomLightTimer;
RBD::Light outArduinoLed;
const unsigned long c_blinkTime = 200; // milliseconds
// The timers for starting a reclosing
RBD::Timer doorUpStartReclose;
RBD::Timer doorUpReallyReclose;
RBD::Timer doorDownPausing;
// The variable to ensure printing only every 8th time some debug output
int g_continuous_printing = 0;
inline void outRoomLightSwitchOn() {
outRoomLightTimer.blink(150, 150, 1);
}
// A flag and a check to make sure to switch off the serial port
// completely, once the first button is pressed
static bool g_initialSerialSwitchedOff = false;
inline void checkInitialSerialSwitchOff() {
if (!g_initialSerialSwitchedOff) {
g_initialSerialSwitchedOff = true;
#ifdef DEBUGOUTPUT
Serial.println("Non-debug build would switch off serial now, but in debug we dont do this");
#else
Serial.end();
// From https://stackoverflow.com/questions/52115371 - how to not only disable
// the interrupts, but also the serial monitor:
// Save status register, disable interrupts
uint8_t oldSREG = SREG;
cli();
// Disable TX and RX
cbi(UCSR0B, RXEN0);
cbi(UCSR0B, TXEN0);
// Disable RX ISR
cbi(UCSR0B, RXCIE0);
// Flush the internal buffer
Serial.flush();
// Restore status register
SREG = oldSREG;
#endif
}
}
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(pinOutMotorOn, OUTPUT);
pinMode(pinOutMotorUp, OUTPUT);
outWarnLightTimer.setupPin(pinOutWarnLight);
outWarnLightTimer.fade(c_blinkTime, c_blinkTime, c_blinkTime, c_blinkTime, 2);
outRoomLightTimer.setupPin(pinOutRoomLight, true, true); // digital=true, inverted=true
outRoomLightSwitchOn();
outArduinoLed.setupPin(pinOutArduinoLedPin, true); // digital=true
outArduinoLed.blink(2000, 2000); // In DOOR_DOWN, do some slow blinking to show we are alive
digitalWrite(pinOutMotorOn, RELAY_OFF);
digitalWrite(pinOutMotorUp, RELAY_OFF);
pinMode(pinInButton, INPUT_PULLUP);
inButtonDebounce.attach(pinInButton);
inButtonDebounce.interval(c_debounceDelay);
pinMode(pinInButtonDown, INPUT_PULLUP);
inButtonDownDebounce.attach(pinInButtonDown);
inButtonDownDebounce.interval(c_debounceDelay);
pinMode(pinInLightswitch, INPUT_PULLUP);
// The input button that has a potentiometer behaviour (due to humidity)
inButtonOutsideDebounce.attach(pinAnalogInOutsideButton);
inButtonOutsideDebounce.interval(c_debounceDelay);
inButtonOutsideDebounce.setCurrentAsMax(); // calibrate the current "high" value of the outside button
#ifdef DEBUGOUTPUT
Serial.begin(9600);
#endif
doorUpStartReclose.setTimeout(c_waitingTimeBeforeReclose);
doorUpReallyReclose.setTimeout(c_waitingTimeBeforeReallyReclose);
doorDownPausing.setTimeout(c_movingDownPause);
doorUpStartReclose.stop();
doorUpReallyReclose.stop();
doorDownPausing.stop();
}
inline void transitionTo_MOVING_DOWN_PAUSED() {
state = DOOR_MOVING_DOWN_PAUSED;
digitalWrite(pinOutMotorOn, RELAY_OFF);
outWarnLightTimer.fade(c_blinkTime, c_blinkTime, c_blinkTime, c_blinkTime);
doorDownPausing.restart();
}
// the loop routine runs over and over again forever:
void loop() {
// Update for the input buttons
const bool onInButtonOutsideChanged = inButtonOutsideDebounce.update();
const bool onInButtonOutsidePressed = onInButtonOutsideChanged && (inButtonOutsideDebounce.read() == LOW);
const bool onInButtonChanged = inButtonDebounce.update();
const bool onInButtonPressed = onInButtonChanged && (inButtonDebounce.read() == LOW);
const bool onInButtonDownChanged = inButtonDownDebounce.update();
const bool onInButtonDownPressed = onInButtonDownChanged && (inButtonDownDebounce.read() == LOW);
const bool onInLightswitchBlocked = (digitalRead(pinInLightswitch) == HIGH);
if (onInLightswitchBlocked)
{
g_lastLightswitchBlocked = millis();
}
// Update for the timed output LED
outWarnLightTimer.update();
outRoomLightTimer.update();
outArduinoLed.update();
#ifdef DEBUGOUTPUT
int val;
g_continuous_printing++;
if ((g_continuous_printing & 0xFFF) == 0) {
Serial.print(" state = ");
Serial.print(state);
Serial.print(" analogInButton= ");
val = analogRead(pinAnalogInOutsideButton);
Serial.print(val);
Serial.print(" inLightswitch = ");
Serial.println(onInLightswitchBlocked);
}
if (onInButtonOutsideChanged) {
Serial.print("onInButtonOutsideChanged -- ");
}
if (onInButtonChanged) {
Serial.print("onInButtonChanged, now = ");
Serial.print(inButtonDebounce.read());
Serial.print(" at millis=");
Serial.println(millis());
}
if (onInButtonDownChanged) {
Serial.print("onInButtonDownChanged, now = ");
Serial.print(inButtonDownDebounce.read());
Serial.print(" at millis=");
Serial.println(millis());
}
#endif
switch (state) {
case DOOR_DOWN:
// Door down/closed: Make sure motor is off
digitalWrite(pinOutMotorOn, RELAY_OFF);
digitalWrite(pinOutMotorUp, RELAY_OFF);
// State change: Only when the Up-Button is pressed
if (onInButtonPressed || onInButtonOutsidePressed) {
state = DOOR_MOVING_UP;
g_lastMoveStart.setValue(millis());
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.println(": state DOWN -> MOVING_UP");
#endif
outWarnLightTimer.on();
outRoomLightSwitchOn();
checkInitialSerialSwitchOff();
}
break;
case DOOR_UP:
// Door up/open: Make sure motor is off
digitalWrite(pinOutMotorOn, RELAY_OFF);
digitalWrite(pinOutMotorUp, RELAY_OFF);
// Special rule on Up-Button, if the reclose blinking is ongoing: Cancel the reclose
if (doorUpReallyReclose.isActive() && (onInButtonPressed || onInLightswitchBlocked)) {
doorUpReallyReclose.stop();
doorUpStartReclose.restart();
outWarnLightTimer.off();
} else
// State change can be because of multiple things
if (onInButtonPressed || onInButtonOutsidePressed || onInButtonDownPressed || doorUpReallyReclose.onExpired()) {
g_lastMoveStart.setValue(millis());
doorUpStartReclose.stop();
doorUpReallyReclose.stop();
g_lastMovePartCompleted = 0;
outRoomLightSwitchOn();
if (onInButtonDownPressed) {
inButtonOutsideDebounce.setCurrentAsMax(); // calibrate the current "high" value of the outside button
}
if (onInLightswitchBlocked || millis() <= g_lastLightswitchBlocked + c_movingDownPause) {
// Oh, we had somebody blocking the lightswitch, so go into PAUSED mode immediately
transitionTo_MOVING_DOWN_PAUSED();
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state DOOR_UP -> MOVING_DOWN_PAUSED due to lightswitchBlocked. lastMoveStart=");
Serial.println(g_lastMoveStart.getValue());
#endif
//delay(1); // maybe this fixes a potential wrong state transition
} else {
// Any button was pressed or timer expired => State change: Now move downwards
state = DOOR_MOVING_DOWN;
outWarnLightTimer.on();
doorDownPausing.stop();
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state DOOR_UP -> MOVING_DOWN; lastMoveStart=");
Serial.println(g_lastMoveStart.getValue());
#endif
}
}
// While the door is open, check for the timer timeout of re-closing
if (doorUpStartReclose.onExpired()) {
doorUpReallyReclose.restart();
outWarnLightTimer.blink(c_blinkTime, c_blinkTime);
outRoomLightSwitchOn();
}
break;
case DOOR_MOVING_DOWN:
if (onInLightswitchBlocked) {
// Light switch is blocked => State change: Go into PAUSED state
transitionTo_MOVING_DOWN_PAUSED();
g_lastMovePartCompleted = millis() - g_lastMoveStart.getValue();
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state MOVING_DOWN -> PAUSED due to lightswitchBlocked. lastMovePartCompleted[%] = ");
Serial.println(currentMoveCompletedToPercent());
#endif
} else {
// Door moving downwards/closing
digitalWrite(pinOutMotorUp, RELAY_OFF);
digitalWrite(pinOutMotorOn, RELAY_ON);
// State change: Have we reached the DOOR_DOWN position?
if (millis() - g_lastMoveStart.getValue() > c_moveDurationTotal) {
#ifdef DEBUGOUTPUT
Serial.print("State change to DOOR_DOWN with lastMoveStart=");
Serial.println(g_lastMoveStart.getValue());
#endif
state = DOOR_DOWN;
outWarnLightTimer.off();
outArduinoLed.blink(2000, 2000); // In DOOR_DOWN, do some slow blinking to show we are alive
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state MOVING_DOWN -> DOOR_DOWN; lastMoveStart=");
Serial.print(g_lastMoveStart.getValue());
Serial.print(" diff=");
Serial.print(millis() - g_lastMoveStart.getValue());
Serial.print(" moveTotal=");
Serial.println(c_moveDurationTotal);
#endif
}
}
// State change: Pressed button for changing direction?
if (onInButtonPressed || onInButtonOutsidePressed) {
// Some up-button was pressed => State change: Now move up again
state = DOOR_MOVING_UP;
digitalWrite(pinOutMotorOn, RELAY_OFF);
g_lastMovePartCompleted = millis() - g_lastMoveStart.getValue();
const unsigned long moveRemaining = c_moveDurationTotal - g_lastMovePartCompleted;
delay(c_moveTurnaroundPause);
g_lastMoveStart.setValue(millis() - moveRemaining);
#ifdef DEBUGOUTPUT
Serial.print("state MOVING_DOWN -> MOVING_UP. lastMovePartCompleted[%] = ");
Serial.println(currentMoveCompletedToPercent());
#endif
} else if (onInLightswitchBlocked) {
// Light switch is blocked => State change: Go into PAUSED state
transitionTo_MOVING_DOWN_PAUSED();
g_lastMovePartCompleted = millis() - g_lastMoveStart.getValue();
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state MOVING_DOWN -> PAUSED due to lightswitchBlocked. lastMovePartCompleted[%] = ");
Serial.println(currentMoveCompletedToPercent());
#endif
delay(1); // otherwise we get a wrong state transition
}
break;
case DOOR_MOVING_DOWN_PAUSED:
digitalWrite(pinOutMotorOn, RELAY_OFF);
digitalWrite(pinOutMotorUp, RELAY_OFF);
// State change: Pressed button for changing direction?
if (onInButtonPressed || onInButtonOutsidePressed) {
// Some button was pressed => State change: Now move up again
state = DOOR_MOVING_UP;
outWarnLightTimer.on();
const unsigned long moveRemaining = c_moveDurationTotal - g_lastMovePartCompleted;
g_lastMoveStart.setValue(millis() - moveRemaining);
#ifdef DEBUGOUTPUT
Serial.print("state PAUSED -> MOVING_UP; lastMovePartCompleted[%] = ");
Serial.print(currentMoveCompletedToPercent());
Serial.print(" lastMoveStart=");
Serial.println(g_lastMoveStart.getValue());
#endif
delay(1); // otherwise we get a wrong state transition
} else if (onInLightswitchBlocked) {
// Light switch is still blocked: Still waiting for pause time
doorDownPausing.restart();
} else if (doorDownPausing.onExpired()) {
// Light switch was free again for long enough => State change: Continue moving downwards
state = DOOR_MOVING_DOWN;
outWarnLightTimer.on();
g_lastMoveStart.setValue(millis() - g_lastMovePartCompleted);
digitalWrite(pinOutMotorOn, RELAY_ON);
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.print(": state PAUSED -> MOVING_DOWN; lastMovePartCompleted[%] = ");
Serial.print(currentMoveCompletedToPercent());
Serial.print("; lastMoveStart=");
Serial.println(g_lastMoveStart.getValue());
#endif
delay(1); // otherwise we get a wrong state transition
}
break;
case DOOR_MOVING_UP:
// Door moving upwards/opening
digitalWrite(pinOutMotorUp, RELAY_ON);
digitalWrite(pinOutMotorOn, RELAY_ON);
// State change: Have we reached the DOOR_UP position?
if (millis() - g_lastMoveStart.getValue() > c_moveDurationTotal) {
state = DOOR_UP;
outWarnLightTimer.off();
inButtonOutsideDebounce.setCurrentAsMax(); // calibrate the current "high" value of the outside button
doorUpStartReclose.setTimeout(c_waitingTimeBeforeReclose);
#ifdef DEBUGOUTPUT
Serial.print("state MOVING_UP -> DOOR_UP; Set restarting timer to timeout=");
Serial.println(c_waitingTimeBeforeReclose);
#endif
doorUpStartReclose.restart();
outArduinoLed.blink(400, 400); // in DOOR_UP state we blink somewhat faster
}
// State change: Pressed button for changing direction?
if (onInButtonDownPressed) {
const unsigned long movePartCompleted = millis() - g_lastMoveStart.getValue();
transitionTo_MOVING_DOWN_PAUSED();
g_lastMovePartCompleted = c_moveDurationTotal - movePartCompleted;
#ifdef DEBUGOUTPUT
Serial.print(millis());
Serial.println(": state MOVING_UP -> MOVING_DOWN_PAUSED; lastMovePartCompleted[%] = ");
Serial.println(currentMoveCompletedToPercent());
#endif
}
break;
}
}