Skip to content

Commit 603428c

Browse files
committed
Add examples
1 parent 60dedcc commit 603428c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
ExternalWakeup
3+
4+
This sketch demonstrates the usage of External Interrupts (on pins) to wakeup a chip in sleep mode.
5+
Sleep modes allow a significant drop in the power usage of a board while it does nothing waiting for an event to happen. Battery powered application can take advantage of these modes to enhance battery life significantly.
6+
7+
In this sketch, shorting pin 8 to a GND will wake up the board.
8+
Please note that, if the processor is sleeping, a new sketch can't be uploaded. To overcome this, manually reset the board (usually with a single or double tap to the RESET button)
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include "ArduinoLowPower.h"
14+
15+
// Blink sequence number
16+
// Declare it volatile since it's incremented inside an interrupt
17+
volatile int repetitions = 1;
18+
19+
// Pin used to trigger a wakeup
20+
const int pin = 8;
21+
22+
void setup() {
23+
pinMode(LED_BUILTIN, OUTPUT);
24+
// Attach a wakeup interrupt on pin 8, calling repetitionsIncrease when the device is woken up
25+
LowPower.attachInterruptWakeup(pin, repetitionsIncrease, CHANGE);
26+
}
27+
28+
void loop() {
29+
for (int i = 0; i < repetitions; i++) {
30+
digitalWrite(LED_BUILTIN, HIGH);
31+
delay(500);
32+
digitalWrite(LED_BUILTIN, LOW);
33+
delay(500);
34+
}
35+
// Triggers an infinite sleep (the device will be woken up only by the registered wakeup sources)
36+
// The power consumption of the chip will drop consistently
37+
LowPower.sleep();
38+
}
39+
40+
void repetitionsIncrease() {
41+
// This function will be called once on device wakeup
42+
// You can do some little operations here (like changing variables which will be used in the loop)
43+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
44+
repetitions ++;
45+
}

examples/TimedWakeup/TimedWakeup.ino

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
TimedWakeup
3+
4+
This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in sleep mode.
5+
Sleep modes allow a significant drop in the power usage of a board while it does nothing waiting for an event to happen. Battery powered application can take advantage of these modes to enhance battery life significantly.
6+
7+
In this sketch, the internal RTC will wake up the processor every 2 seconds.
8+
Please note that, if the processor is sleeping, a new sketch can't be uploaded. To overcome this, manually reset the board (usually with a single or double tap to the RESET button)
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include "ArduinoLowPower.h"
14+
15+
void setup() {
16+
pinMode(LED_BUILTIN, OUTPUT);
17+
// Uncomment this function if you wish to attach function dummy when RTC wakes up the chip
18+
// LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, dummy, CHANGE);
19+
}
20+
21+
void loop() {
22+
digitalWrite(LED_BUILTIN, HIGH);
23+
delay(500);
24+
digitalWrite(LED_BUILTIN, LOW);
25+
delay(500);
26+
// Triggers a 2000 ms sleep (the device will be woken up only by the registered wakeup sources and by internal RTC)
27+
// The power consumption of the chip will drop consistently
28+
LowPower.sleep(2000);
29+
}
30+
31+
void dummy() {
32+
// This function will be called once on device wakeup
33+
// You can do some little operations here (like changing variables which will be used in the loop)
34+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
35+
}

0 commit comments

Comments
 (0)