Skip to content

Instantly share code, notes, and snippets.

@joshuajnoble
Created April 21, 2022 19:29
Show Gist options
  • Save joshuajnoble/5ae6686816ff3fdf4773b01721053bf0 to your computer and use it in GitHub Desktop.
Save joshuajnoble/5ae6686816ff3fdf4773b01721053bf0 to your computer and use it in GitHub Desktop.
#include <bluefruit.h>
#define WAKE_LOW_PIN PIN_A0
#define WAKE_HIGH_PIN PIN_A1
#define SLEEPING_DELAY 10000 // sleep after 30 seconds of blinking
void gotoSleep(unsigned long time)
{
// shutdown when time reaches SLEEPING_DELAY ms
if ((time>SLEEPING_DELAY))
{
// to reduce power consumption when sleeping, turn off all your LEDs (and other power hungry devices)
digitalWrite(LED_BUILTIN, LOW);
// setup your wake-up pins.
pinMode(WAKE_LOW_PIN, INPUT_PULLUP_SENSE); // this pin (WAKE_LOW_PIN) is pulled up and wakes when externally connected to ground.
pinMode(WAKE_HIGH_PIN, INPUT_PULLDOWN_SENSE); // this pin (WAKE_HIGH_PIN) is pulled down and wakes when externally connected to 3.3v.
// power down nrf52.
sd_power_system_off(); // this function puts the whole nRF52 to deep sleep (no Bluetooth)
}
}
// the setup function runs once when you press reset or power the board
void setup() {
Bluefruit.begin(); // Sleep functions need the softdevice to be active.
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalToggle(LED_BUILTIN); // turn the LED on (HIGH is the voltage level)
gotoSleep(millis()); // call millis() and pass it to the sleep function. On wake-up, millis will start at 0 again.
delay(1000); // wait for a second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment