-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathATTiny13A-On-Off-Kill.ino
286 lines (217 loc) · 8.45 KB
/
ATTiny13A-On-Off-Kill.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
/* FILENAME: ATTiny13A-On-Off-Kill.ino
* ORIGINAL FILENAME: ATTiny85-On-Off.cpp
* VERSION: 3.5
* AUTHOR(s):
* ATTiny13A VERSION adapted by Jessica https://accessiblepixel.com
* ORIGINAL VERSION by Ralph Bacon https://github.com/RalphBacon/173-ATTiny85-Push-Button-On-Off-control
* CONTACT: media@accessiblepixel.com
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------------------------------------
*
* Pinout for ATMEL ATTINY 13/13A
* Pin 1 is RESET.
*
* +-\/-+
* PB5 1|o |8 Vcc
* [KILL (to/from external µC)] PB3 2| |7 PB2 [Green 'Power' LED]
* [N-Channel MOSFET Gate] PB4 3| |6 PB1 [Button (Interrupt)]
* GND 4| |5 PB0 [Red 'HALT' LED]
* +----+
*/
#include "Arduino.h"
#include <avr/interrupt.h>
// Interrupt pin PB1 aka PCINT1 aka physicl pin 6. is
// the only pin we can configure as an interrupt on the ATTiny13
#define INT_PIN PB1
// RED LED on PB0 to indicate when shutdown requests are not accepted
// and to show when KILL is waiting for you to release the button.
#define HALT_LED PB0
// Output pin to N-Channel MOSfET
#define PWR_PIN PB4
// Power ON LED pin (will flash on shutdown) pin 5
#define PWR_LED PB2
// KILL interrupt for external µC
#define KILL PB3
// ISR handled variables
volatile bool togglePowerRequest = false;
bool shutDownAllowed = false;
// State of output ON/OFF
bool powerIsOn = false;
bool shutDownInProgress = false;
// When did we switch ON (so we can delay switching off)
unsigned long millisOnTime = 0;
// How long to hold down button to force turn off (ms)
#define KILL_TIME 5000
// Minimum time before shutdown requests accepted (ms)
#define MIN_ON_TIME 5000
// Define time between state changes of flashing LED notifications (ms)
word timeToFlashLED = 125;
// When did we press and hold the OFF button
unsigned long millisOffTime = 0;
// Forward declaration(s)
void powerDownLedFlash();
void shutDownPower();
void waitingToKill();
//===================================
// SETUP SETUP SETUP SETUP
//===================================
void setup(){
// Keep the KILL command a WEAK HIGH until we need it
pinMode(KILL, INPUT_PULLUP);
// Keep the MOSFET switched OFF initially
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
// Enable LED pins as outputs.
pinMode(PWR_LED, OUTPUT);
pinMode(HALT_LED, OUTPUT);
// Power ON Led
digitalWrite(PWR_LED, LOW);
// Interrupt pin when we press power pin again
pinMode(INT_PIN, INPUT_PULLUP);
// None of the 'original' Arduino way of setting an intterupt works here.
// and using the MCUCR and GIMISK commands specific for the ATTiny85 didn't translate
// across to the ATTiny13A correctly. I also changed where the interrupt is defined, as
// it would trigger the interrupt if you kept the power switch pressed on startup.
// Time to power up
togglePowerRequest = true;
}
//=============================
// LOOP LOOP LOOP LOOP
//=============================
void loop() {
// Only after a few seconds do we allow shutdown requests
if (millis() >= millisOnTime + MIN_ON_TIME && powerIsOn)
if (!shutDownAllowed) {
shutDownAllowed = true;
// Enable interrupts here (ATTiny13A Specific) after MIN_ON_TIME has been
// exceeded to avoid false tripping of the ISR.
MCUCR = (MCUCR & ~(_BV(ISC00) | (ISC01))) | (FALLING << ISC00);
GIMSK |= _BV(INT0);
// Turn off the RED LED so that visually you can see when shutdown
// requests are accepted.
digitalWrite(HALT_LED,LOW);
}
// Button was pressed and no shut down already running
if (togglePowerRequest && !shutDownInProgress) {
// Is the power currently ON?
if (powerIsOn) {
// And we can switch off after initial delay period
if (shutDownAllowed) {
shutDownInProgress = true;
// Send the 100mS KILL command to external processor
digitalWrite(KILL, LOW);
pinMode(KILL, OUTPUT);
delay(100);
// Now we wait for confirmation from ext. µC
pinMode(KILL, INPUT_PULLUP);
digitalWrite(KILL, HIGH);
// Kill INT0 ext interrupt as we don't need it again
// until shutdown completed (when we can power up again)
GIMSK &= ~_BV(INT0);
}
}
else {
// Set Power pin to MOSFET gate to high to turn on N-Channel MOSFET
digitalWrite(PWR_PIN, HIGH);
// Turn on Power LED pin to indicate we are running
digitalWrite(PWR_LED, HIGH);
// Power is ON
// Illuminate RED LED to show that no shutdown requests until after MIN_ON_TIME milliseconds
digitalWrite(HALT_LED,HIGH);
millisOnTime = millis();
// Set the flag that power is now ON
powerIsOn = true;
}
// Clear the button press state
togglePowerRequest = false;
}
// IF we are shutting down do not accept further presses
// and wait for confirmation from external µC before killing power
if (shutDownInProgress) {
// Flash the power LED to indicate shutdown request
powerDownLedFlash();
// If we get the KILL command from external µC shutdown power
if (digitalRead(KILL) == LOW) {
// External µC has confirmed KILL power
// we can now shut off the power
shutDownPower();
}
// If the power switch is held down long enough KILL anyway
if (digitalRead(INT_PIN) == HIGH) {
// Button released so reset start time
millisOffTime = 0;
} else {
// If we haven't captured start of long press do it now
if (millisOffTime == 0) {
millisOffTime = millis();
}
// Has the button been pressed long enough to KILL power?
if (millis() > millisOffTime + KILL_TIME) {
// Turn off the power (Instant Off)
shutDownPower();
}
}
}
}
// Interupt Service Routine (ATTiny13 Specific)
ISR(INT0_vect) {
// Keep track of when we entered this routine
static volatile unsigned long oldMillis = 0;
// Switch has closed.
// Check for debounce ~10ms should be adequate.
if (millis() > oldMillis + 10) {
togglePowerRequest = true;
oldMillis = millis();
}
}
// Flash the POWER ON LED to indicate shutting down. this just toggles the LED
// on and off every timeToFlashLED milliseconds. A nice indication that we are shutting down.
void powerDownLedFlash() {
static unsigned long flashMillis = millis();
if (powerIsOn) {
if (millis() > flashMillis + timeToFlashLED) {
digitalWrite(PWR_LED, !digitalRead(PWR_LED));
flashMillis = millis();
}
}
}
// Flash the Status LED to indicate shutdown is complete, and that you should release the
// button to disconnect power. This also uses timeToFlashLED milliseconds.
void waitingToKill() {
static unsigned long flashMillis = millis();
if (millis() > flashMillis + timeToFlashLED) {
digitalWrite(HALT_LED, !digitalRead(HALT_LED));
flashMillis = millis();
}
}
// Common power shutdown routine.
void shutDownPower() {
// Reset all flags to initial state
powerIsOn = false;
shutDownAllowed = false;
shutDownInProgress = false;
millisOffTime = 0;
// Wait for button to be released or we will start up again!
while (digitalRead(INT_PIN) == LOW) {
waitingToKill();
delay(10);
}
// Turn off indicator LEDs and MOSFET
digitalWrite(PWR_PIN, LOW);
digitalWrite(HALT_LED, LOW);
digitalWrite(PWR_LED, LOW);
}