arduino mkr1500 interrupt+sleep

Good morning,
Lately we have been experimenting with projects with ATmega328P microcontrollers to SAMD21, in this case with the MKR1500 board.
My need is to send the board in deep sleep to save consumption and in the meantime to count the impulses of a reed sensor connected to a digital pin via an interrupt.
Here problems arise, with this board (at least in my case) when it goes to sleep, interrupts are not counted as arriving in FALLING / RISING mode, instead with HIGH or LOW mode, the board wakes up at the first impulse ...
Instead I would like the board to continue to sleep for a certain time, and in the meantime to count the impulses.

My piece of code is as follows:

pinMode(countPin, INPUT_PULLUP);

attachInterrupt(digitalPinToInterrupt(countPin), count_int, FALLING); /*attiva interrupt su pin*/
Serial1.println("Go to sleep...");
LowPower.deepSleep(10000);
detachInterrupt(1);                    /*disattiva interrupt su pin*/
print_count();
void count_int()
{
  count++;
}

void print_count(void)
{ 
  Serial1.print("count:  ");
  Serial1.println(count);
  count=0;
}

I apologize in advance for my bad English

Instead I would like the board to continue to sleep for a certain time, and in the meantime to count the impulses.

You know what you describe here? You want the board to sleep (consume no power) but does the job anyway? That doesn't work.

My piece of code is as follows:

We don't want to see pieces of code but complete code! In most cases the error is within that part of the code that you're hiding from us.

Hi,thanks for the reply.

This is the code:

#include <ArduinoLowPower.h>
#define PluvIntPin 0                    
int count = 0;                             

void pluv_int()
{
  count++;
  delay(100);
}

void pluvio(void){
  
  Serial1.print("count:  ");
  Serial1.println(count);

  count=0;
}

void setup() {
  Serial1.begin(115200);
  while (!Serial1.available()); 
  pinMode(PluvIntPin, INPUT_PULLUP);
  
  Serial1.println("Arduino MKR sleep test...\n");
  delay(5000);

}

void loop() {
 
    attachInterrupt(digitalPinToInterrupt(PluvIntPin), pluv_int, FALLING); 
    Serial1.println("Go to sleep for counting...");
    LowPower.deepSleep(10000);
    detachInterrupt(1);                  
    pluvio();

}

pylon:
You know what you describe here? You want the board to sleep (consume no power) but does the job anyway? That doesn't work.

We don't want to see pieces of code but complete code! In most cases the error is within that part of the code that you're hiding from us.

Is there a way to count the pulses while the board is sleeping? or is there anyway the possibility of counting keeping the consumption as low as possible?

Is there a way to count the pulses while the board is sleeping?

To my knowledge: no.

BTW: are we talking about the MKR1500 or an ATmega328p based board? As far as I know the wake up with a level interrupt only is only true for the ATmega328p, the SAM can wake up with any interrupt configured by the interrupt controller.

is there anyway the possibility of counting keeping the consumption as low as possible?

The SAM has the option to enter idle state immediately when it exits the interrupt handler.

Right now we are talking about the MKR1500,but my target is to port ATmega328p based code on MKR1500 board.
Old code for ATmega328p is this:

void wakeUp()
{
  count++;
  delay(100);
}

attachInterrupt(1, wakeUp, FALLING);  
  delay(5000);
  old_time=5*1000+millis();
  while (millis() < old_time) {
    LowPower.idle(SLEEP_1S, ADC_OFF, TIMER2_OFF, TIMER1_ON, TIMER0_ON, SPI_OFF, USART0_OFF, TWI_OFF);
  }
  detachInterrupt(1);

in practice counting the pulses via interrupt while the board was in an idle state, but without waking up.
Because in my tests the MKR1500 board when in sleep as soon as it receives an impulse it wakes up instead of counting and staying in sleep

in practice counting the pulses via interrupt while the board was in an idle state, but without waking up.

How should that work? If the CPU doesn't wake up it cannot count anything.

Because in my tests the MKR1500 board when in sleep as soon as it receives an impulse it wakes up instead of counting and staying in sleep

Sure it wakes up but you can tell it to go to sleep again immediately after you exit the interrupt handler. Post the code you tried on the MKR1500!

How should that work? If the CPU doesn't wake up it cannot count anything.

With the ATmega328p, the board probably wakes up as you say, but does not continue the code but remains dormant until the time set for the LowPower.idle function passes (...);

Sure it wakes up but you can tell it to go to sleep again immediately after you exit the interrupt handler. Post the code you tried on the MKR1500!

How is it possible to do what you say?

My test code is this:

#include <ArduinoLowPower.h>
#define PluvIntPin 0                    
int count = 0;                             

void pluv_int()
{
  count++;
  delay(100);
}

void pluvio(void){
  
  Serial1.print("count:  ");
  Serial1.println(count);

  count=0;
}

void setup() {
  Serial1.begin(115200);
  while (!Serial1.available()); 
  pinMode(PluvIntPin, INPUT_PULLUP);
  
  Serial1.println("Arduino MKR sleep test...\n");
  delay(5000);

}

void loop() {
 
    attachInterrupt(digitalPinToInterrupt(PluvIntPin), pluv_int, FALLING); 
    Serial1.println("Go to sleep for counting...");
    LowPower.deepSleep(10000);
    detachInterrupt(1);                  
    pluvio();

}

But beyond the problem of waking up from sleep, the problem remains that the board does not hear the interrupt with the falling clause ...

With the ATmega328p, the board probably wakes up as you say, but does not continue the code but remains dormant until the time set for the LowPower.idle function passes (...);

Bullshit. The time period is simply used to set the watchdog timer to wake up after that time if no other wake event occurred.

How is it possible to do what you say?

Did you read the SAMD21 datasheet, especially the part about the power manager? If you want to do special task as you're asking for, using high level libraries as the LowPower library doesn't work anymore.

But beyond the problem of waking up from sleep, the problem remains that the board does not hear the interrupt with the falling clause ...

On the ATmega328p or the SAMD21?