Skip to content

Commit 2600fb5

Browse files
committed
add sleep and wdt examples
1 parent 6321cba commit 2600fb5

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
compile:
2+
libraries: ~
3+
platforms:
4+
- uno
5+
- leonardo
6+
7+
unittest:
8+
libraries: ~
9+
platforms:
10+
- uno
11+
- leonardo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <avr/sleep.h>
2+
3+
#define BUTTON_INT_PIN 2
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
Serial.println("start");
8+
delay(200);
9+
pinMode(BUTTON_INT_PIN, INPUT_PULLUP);
10+
attachInterrupt(digitalPinToInterrupt(BUTTON_INT_PIN), isrButtonTrigger, FALLING);
11+
}
12+
13+
void loop() {
14+
// sleep unti an interrupt occurs
15+
sleep_enable(); // enables the sleep bit, a safety pin
16+
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
17+
sleep_cpu(); // here the device is actually put to sleep
18+
sleep_disable(); // disables the sleep bit, a safety pin
19+
20+
Serial.println("interrupt");
21+
delay(200);
22+
}
23+
24+
void isrButtonTrigger() {
25+
// nothing to do, wakes up the CPU
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
compile:
2+
libraries: ~
3+
platforms:
4+
- uno
5+
- leonardo
6+
7+
unittest:
8+
libraries: ~
9+
platforms:
10+
- uno
11+
- leonardo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <avr/wdt.h>
2+
3+
void setup() {
4+
pinMode(LED_BUILTIN, OUTPUT);
5+
digitalWrite(LED_BUILTIN, LOW);
6+
7+
wdt_enable(WDTO_4S);
8+
// First timeout executes interrupt, second does reset.
9+
// So first LED 4s off
10+
// then LED 4s on
11+
// then reset CPU and start again
12+
WDTCSR |= (1 << WDIE);
13+
}
14+
15+
void loop() {
16+
// the program is alive...for now.
17+
wdt_reset();
18+
19+
while (1)
20+
; // do nothing. the program will lockup here.
21+
22+
// Can not get here
23+
}
24+
25+
ISR (WDT_vect) {
26+
// WDIE & WDIF is cleared in hardware upon entering this ISR
27+
digitalWrite(LED_BUILTIN, HIGH);
28+
}
29+

0 commit comments

Comments
 (0)