-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattiny1607_speaker_fingerbot.ino
More file actions
348 lines (279 loc) · 7.6 KB
/
attiny1607_speaker_fingerbot.ino
File metadata and controls
348 lines (279 loc) · 7.6 KB
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
/*
Speaker Watchdog - ATtiny1607
Monitors speaker power LED via LDR + comparator.
When LED goes OFF → wake → servo presses Fingerbot → wait → sleep.
Target: ATtiny1607
Core: megaTinyCore (https://github.com/SpenceKonde/megaTinyCore)
Hardware:
PC2 (pin 19) - Trigger input (comparator + button, shared via R8)
PA5 (pin 6) - Servo PWM (TCA0 WO5)
PB2 (pin 14) - TX (debug serial, TX-only)
Author: Saurabh Datta
Date: February 2026
License: MIT
*/
#include <avr/sleep.h>
#include <Servo_megaTinyCore.h>
// ==================== CONFIGURATION ====================
// See README.md for full documentation
// Debug output over serial (TX-only, 115200 baud)
// Comment out for production to save power
// #define DEBUG_ENABLED
// Trigger polarity - depends on which LDR module you're using:
// true = off-shelf test module (PC2 HIGH when LED OFF)
// false = our custom PCB (PC2 LOW when LED OFF)
#define INVERT_TRIGGER false
// Servo positions in degrees - calibrate for your Fingerbot setup
#define SERVO_REST 30 // Resting position (not touching button)
#define SERVO_PRESS 76 // Press position (pushing Fingerbot button)
// Timing in milliseconds
#define DEBOUNCE_MS 50 // Wait after wake before validating trigger
#define PRESS_HOLD_MS 1600 // How long servo holds the press position
#define PRESS_SETTLE_MS 1000 // Wait for servo to return to rest before detach
#define SERVO_INIT_MS 1000 // Initial servo settle time at boot
#define BOOT_WAIT_MS 8000 // Wait for speaker boot (covers LED dance)
#define SERVO_PRESS_AWAIT 2000
// ==================== PIN DEFINITIONS ====================
#define TRIGGER_PIN PIN_PC2
#define SERVO_PIN PIN_PA5
// ==================== GLOBALS ====================
Servo servo;
volatile byte triggered = 0;
bool serialActive = false; // Track if Serial was started
// ==================== ISR ====================
ISR(PORTC_PORT_vect) {
byte flags = VPORTC.INTFLAGS;
PORTC.INTFLAGS = flags;
triggered = 1;
}
// ==================== HARDWARE HELPERS ====================
void disableUnusedPins() {
// PORTA: PA0=UPDI(skip), PA5=SERVO(skip), rest unused
PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
// PORTB: PB0/PB1=TWI (handled by disableTWI), PB2=TX, PB3=RX (handled by disableSerialPins), rest unused
PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
PORTB.PIN5CTRL = PORT_PULLUPEN_bm;
// PORTC: PC2=TRIGGER(skip), rest unused
PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
PORTC.PIN4CTRL = PORT_PULLUPEN_bm;
PORTC.PIN5CTRL = PORT_PULLUPEN_bm;
}
void disableSerialPins() {
PORTB.DIRSET = PIN2_bm;
PORTB.DIRSET = PIN3_bm;
cli();
PORTB.OUT &= ~PIN2_bm;
PORTB.OUT &= ~PIN3_bm;
sei();
}
void disableTWI() {
// Set TWI pins (PB0, PB1) as OUTPUT LOW for lowest power
PORTB.DIRSET = PIN0_bm;
PORTB.DIRSET = PIN1_bm;
cli();
PORTB.OUT &= ~PIN0_bm;
PORTB.OUT &= ~PIN1_bm;
sei();
}
void disablePeripherals() {
ADC0.CTRLA &= ~ADC_ENABLE_bm;
SPI0.CTRLA &= ~SPI_ENABLE_bm;
}
void setupTriggerPin() {
#if INVERT_TRIGGER
PORTC.PIN2CTRL = PORT_ISC_RISING_gc;
#else
PORTC.PIN2CTRL = PORT_ISC_FALLING_gc;
#endif
}
void disableTriggerInterrupt() {
PORTC.PIN2CTRL &= ~PORT_ISC_gm;
}
void enableTriggerInterrupt() {
#if INVERT_TRIGGER
PORTC.PIN2CTRL = PORT_ISC_RISING_gc;
#else
PORTC.PIN2CTRL = PORT_ISC_FALLING_gc;
#endif
}
void clearTriggerFlags() {
PORTC.INTFLAGS = PIN2_bm;
triggered = 0;
}
// ==================== TRIGGER VALIDATION ====================
bool isValidTrigger() {
bool pinHigh = (PORTC.IN & PIN2_bm);
#if INVERT_TRIGGER
return pinHigh;
#else
return !pinHigh;
#endif
}
// ==================== SERVO ====================
void servoPress() {
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Servo: attach, move to PRESS ("));
Serial.print(SERVO_PRESS);
Serial.println(F(" deg)"));
}
#endif
servo.attach(SERVO_PIN);
servo.write(SERVO_PRESS);
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Servo: hold "));
Serial.print(PRESS_HOLD_MS);
Serial.println(F("ms"));
}
#endif
delay(PRESS_HOLD_MS);
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Servo: move to REST ("));
Serial.print(SERVO_REST);
Serial.println(F(" deg)"));
}
#endif
servo.write(SERVO_REST);
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Servo: settle "));
Serial.print(PRESS_SETTLE_MS);
Serial.println(F("ms"));
}
#endif
delay(PRESS_SETTLE_MS);
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.println(F("Servo: detach"));
}
#endif
servo.detach();
pinMode(SERVO_PIN, INPUT_PULLUP);
}
// ==================== MAIN HANDLER ====================
void handleWakeUp() {
#ifdef DEBUG_ENABLED
Serial.begin(115200);
serialActive = true;
delay(10);
Serial.println(F(""));
Serial.println(F("=== WAKE ==="));
#endif
// 2. Debounce
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Debounce: "));
Serial.print(DEBOUNCE_MS);
Serial.println(F("ms"));
}
#endif
delay(DEBOUNCE_MS);
#ifdef DEBUG_ENABLED
if (serialActive) {
bool pinState = (PORTC.IN & PIN2_bm);
Serial.print(F("PC2: "));
Serial.println(pinState ? "HIGH" : "LOW");
}
#endif
if (!isValidTrigger()) {
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.println(F("FALSE TRIGGER"));
}
#endif
return;
}
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.println(F("VALID TRIGGER"));
Serial.println(F("Disable PC2 interrupt"));
}
#endif
// 3. Disable interrupt
disableTriggerInterrupt();
// ** NOTE: Add some await before Servo press
// Or else if we press immediately after the speaker gets OFF (speaker LED OFF),
// then pressing it, to turn it ON doesn't work
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Waiting for: "));
Serial.print(SERVO_PRESS_AWAIT);
Serial.println(F("ms before pressing servo"));
}
#endif
delay(SERVO_PRESS_AWAIT);
//============================= //
// 4. Servo press
servoPress();
// 5. Fixed wait
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.print(F("Boot wait: "));
Serial.print(BOOT_WAIT_MS);
Serial.println(F("ms"));
}
#endif
delay(BOOT_WAIT_MS);
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.println(F("Boot wait done"));
}
#endif
}
// ==================== SETUP ====================
void setup() {
// 1. Disable serial pins
disableSerialPins();
// 2. Disable unused pins
disableUnusedPins();
// 2b. Disable TWI pins (PB0, PB1 as OUTPUT LOW)
disableTWI();
// 3-4. Disable peripherals
disablePeripherals();
// 5. Setup trigger pin
setupTriggerPin();
// 6-9. Initialize servo to rest, then detach
servo.attach(SERVO_PIN);
servo.write(SERVO_REST);
delay(SERVO_INIT_MS);
servo.detach();
pinMode(SERVO_PIN, INPUT_PULLUP);
// 10. Enable interrupts
sei();
// 11-12. Sleep mode setup
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
}
// ==================== LOOP ====================
void loop() {
// 1. Check if triggered
if (triggered) {
triggered = 0;
handleWakeUp();
}
// 6. Re-enable interrupt + cleanup + sleep
enableTriggerInterrupt();
clearTriggerFlags();
#ifdef DEBUG_ENABLED
if (serialActive) {
Serial.println(F("Enable PC2 interrupt"));
Serial.println(F("Clear flags"));
Serial.println(F("Sleep..."));
Serial.flush();
Serial.end();
disableSerialPins();
serialActive = false;
}
#endif
// Sleep
sleep_cpu();
}