-
Notifications
You must be signed in to change notification settings - Fork 0
/
successfull_hold_feature_adding_buttons.ino
273 lines (222 loc) · 9.08 KB
/
successfull_hold_feature_adding_buttons.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
/*
*/
// constants won't change. They're used here to set pin numbers:
const int DCbuttonPin = 2; // the number of the pushbutton pin
const int DCledPin = 3; // the number of the LED pin
const int DCpowerPin = 4; // the number of the LED pin
const int VACbuttonPin = 5;
const int VACledPin = 6;
const int VACpowerPin = 7;
const int STOPbuttonPin = 8;
const int STOPledPin = 9;
const long intervalblink = 1000; // interval at which to blink (milliseconds)
const long intervalflash = 100; // interval at which to blink (milliseconds)
const long intervalcountdown = 30000; // timer to shut down machine (milliseconds)
const long intervalwarning = 15000; // rapidly flashes before shutting down (milliseconds)
const long intervalhold = 1800000;
// Variables will change:
int DCledState = LOW; // the current state of the output pin
int DCbuttonState; // the current reading from the input pin
int lastDCbuttonState = LOW; // the previous reading from the input pin
int DCpowerState = false; //
int DCholdState = false;
int VACledState = LOW; // the current state of the output pin
int VACbuttonState; // the current reading from the input pin
int lastVACbuttonState = LOW; // the previous reading from the input pin
int VACpowerState = false; //
int VACholdState = false;
int STOPledState = LOW;
int STOPbuttonState;
int lastSTOPbuttonState = LOW;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDCDebounceTime = 0; // the last time the output pin was toggled
unsigned long lastVACDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long holdDelay = 500; // hold to activate second setting
unsigned long previousDCMillis = 0; // will store last time LED was updated
unsigned long lastDCTime = 0; // the last time the DCpowerState was changed to true
unsigned long previousVACMillis = 0; // will store last time LED was updated
unsigned long lastVACTime = 0; // the last time the VACpowerState was changed to true
unsigned long lastSTOPDebounceTime = 0;
void setup() {
pinMode(DCbuttonPin, INPUT);
pinMode(DCledPin, OUTPUT);
pinMode(DCpowerPin, OUTPUT);
pinMode(VACbuttonPin, INPUT);
pinMode(VACledPin, OUTPUT);
pinMode(VACpowerPin, OUTPUT);
pinMode(STOPbuttonPin, INPUT);
pinMode(STOPledPin, OUTPUT);
// set initial LED state
digitalWrite(DCledPin, DCledState);
digitalWrite(VACledPin, VACledState);
digitalWrite(STOPledPin, STOPledState);
}
void loop() {
// read the state of the switch into a local variable:
int readingDC = digitalRead(DCbuttonPin);
int readingVAC = digitalRead(VACbuttonPin);
int readingSTOP = digitalRead(STOPbuttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (readingDC != lastDCbuttonState) {
// reset the debouncing timer
lastDCDebounceTime = millis();
}
if ((millis() - lastDCDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readingDC != DCbuttonState) {
DCbuttonState = readingDC;
// only toggle the DCState if the new button state is HIGH, and reset timer
if (DCbuttonState == HIGH) {
DCpowerState = true;
DCholdState = false;
lastDCTime = millis();
}
if ((DCbuttonState == LOW) && (DCpowerState == true) && ((millis() - lastDCTime) > holdDelay)){
DCholdState = true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastDCbuttonState:
lastDCbuttonState = readingDC;
// defines DCpowerState:
if (DCpowerState == true) {
digitalWrite(DCledPin, DCledState);
digitalWrite(DCpowerPin, HIGH);
}
if (DCpowerState == false) {
digitalWrite(DCledPin, LOW);
digitalWrite(DCpowerPin, LOW);
}
if (DCholdState == true) {
DCpowerState = false;
digitalWrite(DCledPin, HIGH);
digitalWrite(DCpowerPin, HIGH);
}
if ((millis() - lastDCTime) > intervalcountdown) {
DCpowerState = false;
}
if ((millis() - lastDCTime) < intervalwarning) {
if ((millis() - previousDCMillis) >= intervalblink) {
previousDCMillis = millis(); //save the last time it blinked
if (DCledState == LOW) {
DCledState = HIGH;
} else {
DCledState = LOW;
}
}}
if ((millis() - lastDCTime) > intervalwarning) {
if ((millis() - previousDCMillis) >= intervalflash) {
previousDCMillis = millis();
if (DCledState == LOW) {
DCledState = HIGH;
} else {
DCledState = LOW;
}
}
}
if ((DCholdState == true) && ((millis() - lastDCTime) > intervalhold)) {
DCpowerState = true;
lastDCTime = millis();
DCholdState = false;
}
// If the switch changed, due to noise or pressing:
if (readingVAC != lastVACbuttonState) {
// reset the debouncing timer
lastVACDebounceTime = millis();
}
if ((millis() - lastVACDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readingVAC != VACbuttonState) {
VACbuttonState = readingVAC;
// only toggle the VACState if the new button state is HIGH, and reset timer
if (VACbuttonState == HIGH) {
VACpowerState = true;
VACholdState = false;
lastVACTime = millis();
}
if ((VACbuttonState == LOW) && (VACpowerState == true) && ((millis() - lastVACTime) > holdDelay)){
VACholdState = true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastVACbuttonState:
lastVACbuttonState = readingVAC;
// defines VACpowerState:
if (VACpowerState == true) {
digitalWrite(VACledPin, VACledState);
digitalWrite(VACpowerPin, HIGH);
}
if (VACpowerState == false) {
digitalWrite(VACledPin, LOW);
digitalWrite(VACpowerPin, LOW);
}
if (VACholdState == true) {
VACpowerState = false;
digitalWrite(VACledPin, HIGH);
digitalWrite(VACpowerPin, HIGH);
}
if ((millis() - lastVACTime) > intervalcountdown) {
VACpowerState = false;
}
if ((millis() - lastVACTime) < intervalwarning) {
if ((millis() - previousVACMillis) >= intervalblink) {
previousVACMillis = millis(); //save the last time it blinked
if (VACledState == LOW) {
VACledState = HIGH;
} else {
VACledState = LOW;
}
}}
if ((millis() - lastVACTime) > intervalwarning) {
if ((millis() - previousVACMillis) >= intervalflash) {
previousVACMillis = millis();
if (VACledState == LOW) {
VACledState = HIGH;
} else {
VACledState = LOW;
}
}
}
if ((VACholdState == true) && ((millis() - lastVACTime) > intervalhold)) {
VACpowerState = true;
lastVACTime = millis();
VACholdState = false;
}
if (readingSTOP != lastSTOPbuttonState) {
// reset the debouncing timer
lastSTOPDebounceTime = millis();
}
if ((millis() - lastSTOPDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readingSTOP != STOPbuttonState) {
STOPbuttonState = readingSTOP;
// only toggle the STOPState if the new button state is HIGH, and reset timer
if (STOPbuttonState == HIGH) {
DCpowerState = false;
DCholdState = false;
VACpowerState = false;
VACholdState = false;
lastDCTime = 0;
lastVACTime = 0;
}
}
}
lastSTOPbuttonState = readingSTOP;
if ((DCpowerState == true) || (VACpowerState == true) || (DCholdState == true) || (VACholdState == true)) {
digitalWrite (STOPledPin, HIGH);
}
if ((DCpowerState == false) && (VACpowerState == false) && (DCholdState == false) && (VAChold 2State == false)) {
digitalWrite (STOPledPin, LOW);
}
}