Low Power mode combined waking up

Hi,

I am using an Arduino Pro Mini for a state of charge battery application but I don't want the device to fully discharge it when not being used for long.

For that, my intention would be to use the "LowPower" library so I can run my code and send it to sleep for 8 seconds in a periodic way. However, I would also like to be able to wake it up by myself using a push-button that also triggers some LED's.

As a basic idea I was thinking in a code like this,

#include "LowPower.h"

// Use pin 2 as wake up pin
const int Button = 12;
int wakeUpPin = digitalPinToInterrupt(Button);

// Used to determine if board woke up from an interrupt
bool interrupt = false;

void wakeUp() 
{
    // Just a handler for the pin interrupt
    interrupt = true;
}

void setup()
{
    Serial.begin(9600);
    pinMode(Button, INPUT);
}


void loop() 
{
    attachInterrupt(wakeUpPin, wakeUp, LOW);

    LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_ON, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
    
    detachInterrupt(wakeUpPin);

    if (interrupt) {
      // Woke up from an interrupt
      interrupt = false;
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
    } else {
        // Woke up by itself
    }
}

Could somebody verify this? I would really appreciate some feedback from the experts :smiley:

Thank you!!

It looks plausible. But you are going to have to “verify” this yourself by…

build and test the wiring and code. A good ammeter is essential to see what the current drawn in sleep you are actually accomplishing.

As you may find, there are things that can sabotage the low power-ness of a design.

You are using a library, nevertheless I think this is required reading for all on this subject:

http://www.gammon.com.au/power

What do you find when you try the code you posted?

a7

Variables shared with interrupt routines must be declared volatile

volatile bool interrupt = false;