Skip to content

Instantly share code, notes, and snippets.

@embedded-creations
Last active June 22, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embedded-creations/0c22db0f5deddd4f30bc2652ae0e1261 to your computer and use it in GitHub Desktop.
Save embedded-creations/0c22db0f5deddd4f30bc2652ae0e1261 to your computer and use it in GitHub Desktop.
Demonstrate bug in System.sleep() - will sleep infinitely long with small values of `seconds`
#include "application.h"
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
// if you want to change sleepTimeSeconds or reset sleepTimeSeconds back to maxSleepTimeSeconds, change the value of magicWordComparison and reflash application
const uint32_t magicWordComparison = 0xDEADBEEF;
const unsigned int maxSleepTimeSeconds = 10;
retained unsigned int sleepTimeSeconds = maxSleepTimeSeconds;
retained uint32_t magicWord = magicWordComparison;
void setup() {
unsigned int currentSleepTimeSeconds;
// simple check for valid retained memory
if(magicWord != magicWordComparison) {
magicWord = magicWordComparison;
sleepTimeSeconds = maxSleepTimeSeconds;
}
currentSleepTimeSeconds = sleepTimeSeconds;
if(--sleepTimeSeconds == 0)
sleepTimeSeconds = maxSleepTimeSeconds;
// wait for serial
delay(10000);
Serial.print("connected, going to sleep for ");
Serial.print(currentSleepTimeSeconds);
Serial.println(" seconds");
System.sleep(SLEEP_MODE_DEEP, currentSleepTimeSeconds);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment