# \[I want a node\] Sleep and Wake for battery longevity

**URL:** <https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825>\
**Category:** Hardware Interfacing Questions\
**Created:** [November 12, 2019, 12:04pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825 "2019-11-12T12:04:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 12, 2019, 12:04pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/1 "2019-11-12T12:04:03Z")

</div>

## sleepwake

When the boolean is true, the board will go to into full sleep waiting for the condition to be false and wake up.

### Inputs

- `COND` (boolean, default=0) — True is asleep, False is awake.

### Outputs

* * *

I am a beginner. I’d like to accomplish sleep and wake, they are ying and yang, two halves of the same thing. I’ve found the [sleepydog](https://xod.io/libs/bradzilla84/sleepydog/sleepydog-sleep/) node, but seems incomplete and hard to understand without extensive background knowedge. I’m not a coder, that’s why I’m here using **XOD, which is absolutely fantastic** , albeit still missing some powerful nodes.

Context: I’m making an automatic chicken door opener that opens and closes with daylight. It works.  
Now I need to conserve the battery because the board only needs to be awake twice a day for door movement.

The best nodes I’ve seen in XOD have been approached with an elegant simplicity for Pete, even if under the hood it’s a complex action.

I have an incline that because it’s a asleep, the patch doesn’t run, so it has to wake up through a pin going high? In which case (although a problem for waking through an LDR wether day or night), this node could receive:

### Inputs

- `PULSE` (pulse, default=0) — Pulse that sleeps the board.
- `PORT` (port, default=2) — Pin on high that wakes the board.

Surely a sleep/wake node is a big deal for projects running on battery?

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 16, 2019, 6:17pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/2 "2019-11-16T18:17:39Z")

</div>

The sleepydog node is really a timer to wake up but we don’t have the same thing based on an interupt pin. e.g. pulse to sleep, and when it gets light or a PIR detects movement, wake up on that pin change.

Regarding the latter two inputs above, I’m trying to adapt this following code into this XOD node: [https://github.com/RalphBacon/Arduino-Deep-Sleep/blob/master/Sleep\_ATMEGA328P.ino#L1](https://github.com/RalphBacon/Arduino-Deep-Sleep/blob/master/Sleep_ATMEGA328P.ino#L1)

Anyone want to help please?

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 20, 2019, 8:53pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/3 "2019-11-20T20:53:50Z")

</div>

Ok so I’ve tried to do this myself anyway for the benefit of anyone who cares.

This throws up errors I don’t understand.  
I’m not sure how to handle the wakeISR interupt event when it happens, should this be in it’s own void wakeISR function here, or should that be in another node?

This code is taken from the github link in my question.

```auto

{{#global}}
#include <avr/sleep.h>
{{/global}}

struct State {
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
         if (isInputDirty<input_SLEEP>(ctx)){

             static byte prevADCSRA = ADCSRA;
             ADCSRA = 0;
             set_sleep_mode (SLEEP_MODE_PWR_DOWN);
             sleep_enable();
             MCUCR = bit (BODS) | bit (BODSE);
             MCUCR = bit (BODS);
             noInterrupts();
             attachInterrupt(digitalPinToInterrupt(<input_WAKE>(ctx)), wakeISR, CHANGE);
             interrupts();
             sleep_cpu();
//is asleep now, waiting for input WAKE port to change, wakeISR should run these two lines:
// sleep_disable();
// detachInterrupt(digitalPinToInterrupt(wakePin));
             ADCSRA = prevADCSRA;
             }
}

```

---

<div class="post-metadata">

**Author:** ![robertspark](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@robertspark](https://forum.xod.io/u/robertspark)\
**Post date:** [November 20, 2019, 10:27pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/4 "2019-11-20T22:27:45Z")

</div>

I was going to suggest you look at intigrating an existing arduino library such as this one:

> **[rocketscream/Low-Power](https://github.com/rocketscream/Low-Power)**
>
> Low Power Library for Arduino. Contribute to rocketscream/Low-Power development by creating an account on GitHub.

or one of the others available via google search.

probably trying to follow some of the existing examples such as this:  
[https://xod.io/docs/guide/wrapping-arduino-libraries/](https://xod.io/docs/guide/wrapping-arduino-libraries/)

I’ve only been using XOD a week, and have had my own issues attempting to integrate what I thought was a “simple” bit of code into XOD and failing … [https://github.com/NicksonYap/digitalWriteFast](https://github.com/NicksonYap/digitalWriteFast)

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 23, 2019, 4:34pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/5 "2019-11-23T16:34:49Z")

</div>

Thanks @robertspark. So many have seen this, my first post and not said hello or tried to help ☹

Regarding my previous code, how do you think calling another function should be done in xod’s void evaluate(Context ctx){} function?

` attachInterrupt(digitalPinToInterrupt(<input_WAKE>(ctx)), wakeISR, CHANGE);`

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [November 27, 2019, 4:52pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/6 "2019-11-27T16:52:03Z")

</div>

> [@cdbradwell](#):
>
> So many have seen this, my first post and not said hello or tried to help ☹

Oh, so sorry for being hostile. Hello and welcome! 🤗

Well, I think you are very close to success. The only thing left to do is inserting the ISR routing. Unfortunatelly, C++ don’t have so-called function closures / partially applied functions out of the box. The best thing you have is a parameterless global function. Which should be enough to solve the problem though.

Haven’t checked it but here’s outline of how it can look like:

```cpp
{{#global}}
#include <avr/sleep.h>
{{/global}}

struct State { };

{{ GENERATED_CODE }}

// To transfer the context to the ISR function, we have to keep the
// interrupt number global. Will set it in `evaluate`
uint8_t g_interruptNum = -1;

// Also store the ADC state to bring it back
uint8_t g_prevADCSRA;

void sleepISR() {
  // Prevent sleep mode, so we don't enter it again, except deliberately, by code
  sleep_disable();

  // Detach the interrupt that brought us out of sleep
  detachInterrupt(g_interruptNum);
  g_interruptNum = -1;

  // Restore ADC state
  ADCSRA = g_prevADCSRA;
}

void evaluate(Context ctx) {
  if (!isInputDirty<input_SLEEP>(ctx))
    return;

  auto wakePort = getValue<input_WAKE>(ctx);
  g_interruptNum = digitalPinToInterrupt(wakePort); // save globally
  g_prevADCSRA = ADCSRA;
  ADCSRA = 0;
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);
  noInterrupts();
  attachInterrupt(g_interruptNum, wakeISR, CHANGE);
  interrupts();
  sleep_cpu();
}

```

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [November 27, 2019, 5:19pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/7 "2019-11-27T17:19:48Z")

</div>

> [@cdbradwell](#):
>
> Thanks @robertspark. So many have seen this, my first post and not said hello or tried to help ☹

hi, it is a bad habit, sometimes I do not understand I do let another user with knowledge say. But beware that there are also users who never thank …😄

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 27, 2019, 10:52pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/8 "2019-11-27T22:52:51Z")

</div>

Thanks @nkrkv ! I see what you’re doing there. That would of taken me weeks to work out, since this is not my background or strength.  
I changed the v`oid sleepISR` to `void wakeISR` and it runs and uploads, but it is not going to sleep.

Should any of the global settings go inside:

```auto
void evaluate(Context ctx) {
    if (isSettingUp()) {

```

or into the `struct State {};` ?

And does the `g_interruptNum` refer to the port number? in which case why is it -1 ?

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [November 28, 2019, 9:31am UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/9 "2019-11-28T09:31:10Z")

</div>

OK. I’ve checked it live and published a library: [nkrkv/avr-sleep — XOD](https://xod.io/libs/nkrkv/avr-sleep/)

> [@cdbradwell](#):
>
> And does the `g_interruptNum` refer to the port number? in which case why is it -1 ?

The `g_interruptNum` keeps the _interrupt_ number. Port numbers and interrupt numbers are not the same. For example, `D2` has interrupt number `0` and `D3` has `1`. `-1` is just a placeholder for invalid value, nothing.

While trying the node yourself, please, keep in mind that you have to use the `WAKE` port capable of handling external interrupts (`D2` and `D3` or Uno and Nano).

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [November 28, 2019, 11:14pm UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/10 "2019-11-28T23:14:48Z")

</div>

That is superb, works a treat! I see you’ve done a few good things to make it a proper node. Like a done pulse and a condition to turn off the interupt pin when not in sleep.  
Curious question: the sleep\_bod\_disable(); before sleep. Is this supposed to be enabled on wake? And does that line replace the MCUCR ?  
Thanks @nkrkv !

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [November 29, 2019, 9:47am UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/11 "2019-11-29T09:47:02Z")

</div>

Glad it solves the problem.

> [@cdbradwell](#):
>
> Curious question: the sleep\_bod\_disable(); before sleep. Is this supposed to be enabled on wake? And does that line replace the MCUCR ?

Yes, it’s instead of MCUCR manipulation: this marco function just makes code a bit more readable. And no, you haven’t to enable the brownout detection when awake, it happens automatically. But you have to call it again right before the next sleep (which is done by the node). See: [avr-libc: \<avr/sleep.h\>: Power Management and Sleep Modes](https://www.nongnu.org/avr-libc/user-manual/group __avr__ sleep.html#gabf889562cc5ea768ee80cfc8a5bb0312)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/eb7348baa04192c2ef932d1db54d44f09f8a7888.png) [@system](https://forum.xod.io/u/system)\
**Post date:** [December 1, 2019, 9:47am UTC](https://forum.xod.io/t/i-want-a-node-sleep-and-wake-for-battery-longevity/2825/12 "2019-12-01T09:47:06Z")

</div>

Closed on December 1, 2019, 9:47am UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
