-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlostModelAlarm.ino
More file actions
75 lines (59 loc) · 2.26 KB
/
Copy pathlostModelAlarm.ino
File metadata and controls
75 lines (59 loc) · 2.26 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
#include <avr/sleep.h>
#include <avr/wdt.h>
const int buzzerPin = 0; // Physical Pin 5 (PB0)
volatile int sleepCounter = 0; // Counts how many 8-second cycles have past
const int targetCycles = 75; // 75 cycles * 8 seconds = 600 seconds (10 minutes)
// Watchdog Timer Interrupt Service Routine
ISR(WDT_vect) {
sleepCounter++; // Increment our 8-second interval counter
}
void setup() {
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
// ─── NEW: PRE-FLIGHT STATUS FEATURE ───
// 1. Wait 10 seconds before giving the status beep
delay(10000);
// 2. Sound a long confirmation beep for 3 seconds
digitalWrite(buzzerPin, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
// ──────────────────────────────────────
setupWatchdog(); // Configure the hardware watchdog timer after the beep
}
void loop() {
// If we haven't reached 10 minutes yet, go back to deep sleep
if (sleepCounter < targetCycles) {
goToSleep();
}
// Once 10 minutes hits, stay awake and execute the loud alarm loop
else {
triggerAlarm();
}
}
void setupWatchdog() {
cli(); // Disable all interrupts while configuring
MCUSR &= ~(1 << WDRF); // Clear the Watchdog Reset Flag
// Start timed sequence to allow changing configuration
WDTCR |= (1 << WDCE) | (1 << WDE);
// Set Watchdog settings: Interrupt mode enabled, Timeout = 8.0 seconds
WDTCR = (1 << WDIE) | (1 << WDP3) | (0 << WDP2) | (0 << WDP1) | (1 << WDP0);
sei(); // Re-enable interrupts
}
void goToSleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set to maximum power savings
sleep_enable(); // Enable the sleep function
sleep_cpu(); // Actually put the MCU to sleep here...
// ─── THE CHIP LIVES HERE IN DEEP SLEEP FOR 8 SECONDS ───
sleep_disable(); // Execution resumes here after WDT barks
}
void triggerAlarm() {
// Active Buzzer Alert Pattern: 2 rapid beeps every 10 seconds
digitalWrite(buzzerPin, HIGH);
delay(150);
digitalWrite(buzzerPin, LOW);
delay(100);
digitalWrite(buzzerPin, HIGH);
delay(150);
digitalWrite(buzzerPin, LOW);
delay(10000);
}