Skip to content

Instantly share code, notes, and snippets.

@arcao
Last active February 13, 2017 01:26
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 arcao/73d5e3b3e1a2a500843d0d6390acacca to your computer and use it in GitHub Desktop.
Save arcao/73d5e3b3e1a2a500843d0d6390acacca to your computer and use it in GitHub Desktop.
Delay like function for LowPower Arduino library

Delay like function for LowPower Arduino library

Here is delay-like function for Low-Power Arduino library. All delay measurments are performed with ATMega328pu (8MHz internal, 3.3V).

Real delays

  • powerDownDelay(60000) = 60002.256ms
  • powerDownDelay(30000) = 30001.9615ms
  • powerDownDelay(15000) = 15001.783ms
  • powerDownDelay(10000) = 10000.08ms
  • powerDownDelay(5000) = 5000.3095ms
  • powerDownDelay(2000) = 2007.191ms
  • powerDownDelay(1000) = 1003.891ms
  • powerDownDelay(500) = 502.1795ms

Note: All checked with a logic analyzer.

Source

void powerDownDelay(unsigned long delay) {
  period_t period;
  
  while(delay > 0) {
    if (delay > 8287) {
      period = SLEEP_8S;
      delay -= 8287;
    } else if (delay > 4144) {
      period = SLEEP_4S;
      delay -= 4144;
    } else if (delay > 2072) {
      period = SLEEP_2S;
      delay -= 2072;
    } else if (delay > 1036) {
      period = SLEEP_1S;
      delay -= 1036;
    } else if (delay > 518) {
      period = SLEEP_500MS;
      delay -= 518;
    } else if (delay > 259) {
      period = SLEEP_250MS;
      delay -= 259;
    } else if (delay > 129) {
      period = SLEEP_120MS;
      delay -= 129;
    } else if (delay > 64) {
      period = SLEEP_60MS;
      delay -= 64;
    } else if (delay > 32) {
      period = SLEEP_30MS;
      delay -= 32;
    } else {
      period = SLEEP_15MS;
      delay -= 16;
    }

    LowPower.powerDown(period, ADC_OFF, BOD_OFF);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment