Low power issue periodic wakeup and 2 different interrupts

Hi,
I'm struggeling with a low power code..

Attached I drew a structogram. I need to two different external interrupts and two different sleeping modes.
My device should go to sleep forever and when triggered input A increase a counter, sleep for 1 second and if input A is still pressed increase again etc till input==low, print out the count result, sleeping forever and wait for a next interrupt. In addition I want to wake up during "forever sleep" and switch on a oled and display a menu till leaving it..

struct.png

Would you recommend using a library or using somethin similar like this example I found

#include <avr/sleep.h>                 
#include <avr/power.h>
#include <avr/wdt.h>

volatile bool pressed = false;

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial.println("starting...");
    delay(100);
   
    pinMode(2, INPUT_PULLUP);
    pinMode(3, INPUT_PULLUP);
   
    // pin change interrupt
    PCMSK2 |= bit (PCINT18)| bit (PCINT19);  // want pin D2 and D3
    PCIFR  |= bit (PCIF2);   // clear any outstanding interrupts
    PCICR  |= bit (PCIE2);   // enable pin change interrupts for D0 to D5
   
    //enable watchdog to check for code lockups
    wdt_enable(WDTO_8S);
}


 
ISR (PCINT2_vect)
{
   // handle pin change interrupt for D2/D3
   pressed = true;
}   


void sleepNow ()
{   
    wdt_disable();

    set_sleep_mode (SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
    byte adcsra_save = ADCSRA;
    ADCSRA = 0;  // disable ADC
    power_all_disable ();   // turn off all modules
    sleep_enable();
    sleep_cpu ();            // now goes to Sleep and waits for the interrupt
    sleep_disable();
    ADCSRA = adcsra_save;  // stop power reduction
    power_all_enable ();   // turn on all modules
}   


void loop()
{
    // put your main code here, to run repeatedly:

    //reset watchdog timer
    wdt_reset();
 
    //print if button pressed
    if (pressed == true)
    {
      pressed = false;
      Serial.println("pressed");
    }

    //do something useful here

   
 
    delay(100);
    sleepNow();
}

struct.png

Hi,
I used the low power lib and tried this code..as far as I see it's working
Do you have and improvements ?

// **** INCLUDES *****
#include "LowPower.h"

const int int_trigger_A = 2; // pd2 int0
const int wakeUpPin = 3;     // pd3 int1

volatile boolean HANDLER_wakeUp = false;
volatile boolean HANDLER_int_trigger = false;
volatile int sleepmode = 1;
long count;
boolean menuactive = true;


void setup()
{
  pinMode(int_trigger_A, INPUT);
  pinMode(wakeUpPin, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Start");
  delay(1000);
}

void loop()
{

  Serial.println("Loop");
  Serial.print(HANDLER_int_trigger); Serial.println(" HANDLER_int_trigger");
  Serial.print(HANDLER_wakeUp); Serial.println(" HANDLER_wakeUp");


  if (HANDLER_int_trigger) {
    while (digitalRead(int_trigger_A) == HIGH) {
      count++;
      Serial.print("count++ ");
      attachInterrupt(0, int_trigger, RISING);
      attachInterrupt(1, wakeUp, FALLING);
      digitalWrite(13, HIGH);
      delay(20);
      digitalWrite(13, LOW);
      delay(20);
      LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
      detachInterrupt(1);
      detachInterrupt(0);
    }

    Serial.print("count "); Serial.println(count);
    count = 0;
    HANDLER_int_trigger = false;
  }

  attachInterrupt(0, int_trigger, RISING);
  attachInterrupt(1, wakeUp, FALLING);
  Serial.print("s2");
  delay(100);
  LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  Serial.println("w2");
  detachInterrupt(1);
  detachInterrupt(0);
  Serial.print(HANDLER_int_trigger); Serial.println(" HANDLER_int_trigger");
  Serial.print(HANDLER_wakeUp); Serial.println(" HANDLER_wakeUp");

  if (HANDLER_wakeUp) {
    int menucount = 0;
    menuactive = true;
    while (menuactive) {
      Serial.print("menu "); Serial.println(menucount);
      delay(500);
      menucount++;
      if (menucount > 10) {
        menuactive = false;
      }

    }
    HANDLER_wakeUp = false;


  }


}

void wakeUp()
{
  HANDLER_wakeUp = true;
}

void int_trigger()
{
  HANDLER_int_trigger = true;
}

I don't see the need of a library of something as straightforward as sleeping, if only because then you know what is actually happening.

But you'd better get the rest of your project working first. Implement the sleep part later.

Thanks for your answer. I started from scratch testing the sleeping part as my main program wasnt working properly.
I'd like to get running the basic structure. Without a library I'm even more confused

My code above is working so far but there is one big issue..
When triggering the menu it sleeps after a couple of seconds(in my demo) when then triggering HANDLER_int_trigger it also triggers the menu.
It should be HANDLER_wakeUp = 0 but it's 1 instead.
I really dont know why? :confused:

That's the result of
-push button for Trigger menu (wakeUp)
-push button for Trigger HANDLER_int_trigger

Only these two but somehow there is a second interrupt which shows menu..

w2
0 HANDLER_int_trigger
1 HANDLER_wakeUp
menu 0
menu 1
menu 2
menu 3
menu 4
menu 5
menu 6
menu 7
menu 8
menu 9
menu 10
Loop
0 HANDLER_int_trigger
0 HANDLER_wakeUp
s2w2
1 HANDLER_int_trigger
1 HANDLER_wakeUp
menu 0
menu 1
menu 2
menu 3
menu 4
menu 5
menu 6
menu 7
menu 8
menu 9
menu 10
Loop
1 HANDLER_int_trigger
0 HANDLER_wakeUp
count++ count++ count++ count++ count++ count 5
s2