arduino is in LOW Power DOWN watchdog 8s several times, i want to use interrupts

Hello,

the LOW POWER DOWN is activatet for 8s and repeatet several times.
Now i want to interrupt the sleep to set one counter +1. the problem is that the interrupt can stop the sleep in the first second of the 8 running seconds and it starts not after 1 second, it starts after 8 seconds so i lost 7 seconds.

can i read the time of the whatchdogtimer to see how long i must sleep aditionally?

I do not want an external timer.

If i use an shorter timer (250 MS) i think the power consuption is rising wile i start the sleep mode instead 300 times 9600 times.

Thanks

the same problem was here

In fact I am using the interrupt for reading frequency of the sensor. I would like to put the Arduino to sleep only with the timer SLEEP_8S; but when an interrupt occurs from the sensor (many time each second depending on lighting condition)the Arduino wakes-up (I tried both pin 2 and 3). Is there a way to power down the Arduino only with timer 2 despite the interrupt being used?

Hi, I know it is a really old Post but I have the exact same question.
Is the question clear enough? I think so but....

Thanks for future help.

This is caused by a bug in your your code. Had you posted the code, we could tell you what it is, but you didn't...

I have a guess - but, post your code (I am particularly interested in the attachInterrupt() call you use to set up the interrupt, but post the whole sketch.

Sorry at this time i'm at the hospital and I haven't any access to my code.
The issue if I understand well, is that the 8s sleep is working like a charm but an external interrupt will of course wakeup the Arduino and break the 8s sleep. (Since interrupt always restart the execution in the main loop.

Assuming you are using a classic AVR, ex, ATmega328p....
My suspicion is that you are attempting to wake on RISING or FALLING, but you can't do that:

Note that recognition of falling or rising
edge interrupts on INT0 or INT1 requires the presence of an I/O clock, described in Section 8.1 “Clock Systems and their
Distribution” on page 24. Low level interrupt on INT0 and INT1 is detected asynchronously.

However, you can wake with a PCINT or low level interrupt on the INTn pins.

gegelebon:
Sorry at this time i'm at the hospital and I haven't any access to my code.
The issue if I understand well, is that the 8s sleep is working like a charm but an external interrupt will of course wakeup the Arduino and break the 8s sleep. (Since interrupt always restart the execution in the main loop.

Okay - maybe I was wrong - your response indicates potentially more fundamental misunderstanding? When an interrupt that wakes the part from sleep finishes, execution returns to right after the part was put to sleep, wherever that may be - not necessarily the main loop.

(also, this is why you put code in github!)

Sorry I have shorten my answer since English isn't a language that im good enough in. Yes I know that an interrupt work like that. But I find the 1st Post of the guy clear, no?
I will try with my word:

I need to count pulse (in my case gaz pulse).
Falling will rise mi isr (where I just put a volatile bool to true).
Then In the next statement (in my case in the main loop just after the lowpower.powerdown(8s,off,off) that's why I have said that in the previous Post) I increment my counter in a nointerrupts scope.
But I want to send the count (over nrf24) each 10 minutes . And I can't rely on how many time I have wake up to compute 10 minutes (10*60/8=75).

That exactly the problem Express in first post

O I realized that I have push a naive version to see the obvious issue. Do not pay attention to comment since they don't match at all to what the code is doing.

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

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#define LOGDEBUG 0

//DEBUG
#if LOGDEBUG==1
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTDEC(x) Serial.print(x, DEC)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_RADIO() radio.printDetails()
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTDEC(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_RADIO()
#endif

/***********************************/
RF24 radio(7,8);

#define LED_PIN 13
#define RF24_CH 25
#define SLEEP_TIME 75

byte addresses[][6] = {"1Node","2Node"};
/***********************************/

const int interruptPin = 3;
volatile int numChangeInterrupt = 0;

void radioSetup(){
radio.begin();
radio.setChannel(RF24_CH);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setCRCLength(RF24_CRC_16);
radio.setRetries(15, 5);
radio.setAutoAck(1);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
DEBUG_RADIO();
DEBUG_PRINTLN("Radio up");
delay(100);
}

void resetRadio()
{
delay(250);
radio.flush_tx();
delay(50);
radio.powerDown();
radio.end();
delay(100);
DEBUG_PRINTLN("Radio down");
}

void blinkLed(int num)
{
DEBUG_PRINT("Blink");
DEBUG_PRINTLN(num);
pinMode(LED_PIN, OUTPUT);
for (int i =0; i<num; i++)
{
digitalWrite(LED_PIN,HIGH);
delay(500);
digitalWrite(LED_PIN,LOW);
delay(500);
}
}

void Interrupt() {
DEBUG_PRINT("Pulse.");
numChangeInterrupt = numChangeInterrupt+1;
}

void setup()
{
delay(500);

#if LOGDEBUG==1
Serial.begin(9600);
printf_begin();
#endif

blinkLed(2);
// Configure wake up pin as input.
// This will consumes few uA of current.
pinMode(interruptPin, INPUT);

attachInterrupt(digitalPinToInterrupt(interruptPin), Interrupt, RISING);
}

void loop()
{
unsigned int sleepCounter = SLEEP_TIME;
for (; sleepCounter > 0; sleepCounter--)
{
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

noInterrupts();
int numChange = numChangeInterrupt;
interrupts();

bool isWriteOk = false;
int numTry = 0;
while ( !isWriteOk && numTry++ < 5 )
{
radioSetup();
isWriteOk = radio.write( &numChange, sizeof(int) );
resetRadio();
}
blinkLed(isWriteOk ? 2 : 1);
}

During night I realize that I have my algorithm but I still one simple answer:
Is there a way to know how many times I have sleep when I wakeup after a low power.powerdown(8s,....)?
Looking at wdt timer just after the wakeup?