-
Notifications
You must be signed in to change notification settings - Fork 0
/
garageOpener.ino
158 lines (121 loc) · 3.08 KB
/
garageOpener.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
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>
#define RELAY 2
#define HALL_OPEN 7
#define HALL_CLOSED 8
#define LIGHT 9
#define INSIDE_BUTTON 10
#define MOTION_SENSOR 15
#define MOVEMENT_BUFFER 3000
const long minute = 60000L;
const long closeWarning = minute*15;
const long closeMax = minute*20;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, LIGHT);
boolean triedAutoShutting = false;
boolean doorMoving = false;
long lastRelayOn = 0;
int relayDelay = 200;
boolean closingSoon = false;
long openTime = 0;
long lastMovement = 0;
Bounce insideButton = Bounce();
void setup() {
Serial.begin(59700);
pinMode(INSIDE_BUTTON, INPUT_PULLUP);
insideButton.attach(INSIDE_BUTTON);
insideButton.interval(20);
pinMode(HALL_CLOSED, INPUT_PULLUP);
pinMode(HALL_OPEN, INPUT_PULLUP);
pinMode(RELAY, OUTPUT);
strip.begin();
strip.show();
delay(MOVEMENT_BUFFER);
}
void loop() {
checkForMovement();
insideButton.update();
//show the state of the door
updateStatusLed();
if (insideButton.fell()){
triggerRelay();
}
if (doorClosed()){
openTime = 0;
closingSoon = false;
triedAutoShutting = false;
}
if (!doorClosed() && !doorMoving2()) {
if (openTime == 0) {
openTime = millis();
Serial.println("door just opened Setting openTime");
Serial.println("");
} else if (millis() > (openTime + (closeMax)) && !triedAutoShutting){
Serial.println("shutting door");
Serial.println();
triggerRelay();
triedAutoShutting = true;
} else if (millis() > (openTime + (closeWarning))){
closingSoon = true;
} else {
closingSoon = false;
}
}
clearRelay();
}
void checkForMovement() {
if (digitalRead(MOTION_SENSOR) == HIGH) {
lastMovement = millis();
if (openTime != 0){
openTime = lastMovement;
}
}
}
int brightness = 1;
int fadeAmount = 1;
void updateStatusLed(){
if (millis() % 12 == 0){
if (triedAutoShutting && !doorMoving2()){
strip.setPixelColor(0, 255,0,0);
} else if (!doorMoving2()){
strip.setBrightness(255);
if (!doorClosed()){
if(closingSoon){
strip.setPixelColor(0, 0,0,255);
} else {
strip.setPixelColor(0, 255,255,255);
}
} else if (doorClosed()){
strip.setPixelColor(0, 0,0,0);
}
}
else {
strip.setPixelColor(0,0, 255,0);
strip.setBrightness(brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 1 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
}
strip.show();
}
}
void triggerRelay(){
digitalWrite(RELAY, HIGH);
lastRelayOn = millis();
}
void clearRelay(){
if ((millis() - lastRelayOn) > relayDelay) {
digitalWrite(RELAY, LOW);
}
}
boolean doorClosed(){
return digitalRead(HALL_CLOSED) == LOW;
}
boolean doorOpen(){
return digitalRead(HALL_OPEN) == LOW;
}
boolean doorMoving2() {
return (!doorOpen() && !doorClosed()) || lastRelayOn + MOVEMENT_BUFFER > millis();
}