Could you help me with sleep mode and watchdog ?

Hello!

I’m creating a sketch which Arduino will run over batteries, so, low power is a must have.

I copied a sketch from internet, however, the max. time the watchdog timer takes to reset my Arduino can be set to 8 seconds.

I need my Arduino to execute the code inside the MyFunction() once every 5 minutes, THEN go sleep.

How can I prolongue the Sleep mode for about 5 minutes?

Thank you so much.

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


const byte LED = 5;
  
// watchdog interrupt
ISR (WDT_vect) 
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect


void MyFunction(){
  
  pinMode (LED, OUTPUT);
  digitalWrite (LED, HIGH);
  delay (50);
  digitalWrite (LED, LOW);
  pinMode (LED, INPUT);
  
  }

 
void setup () {

}

void loop () 
{


 MyFunction();



  
  // disable ADC
  ADCSRA = 0;  

  // clear various "reset" flags
  MCUSR = 0;     
  // allow changes, disable reset
  WDTCSR = bit (WDCE) | bit (WDE);
  // set interrupt mode and an interval 
  WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 second delay
  wdt_reset();  // pat the dog
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  noInterrupts ();           // timed sequence follows
  sleep_enable();
 
  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS); 
  interrupts ();             // guarantees next instruction executed
  sleep_cpu ();  
  
//  cancel sleep as a precaution
  sleep_disable();
  
  } // end of loop

HINT :

How many 8 Seconds are there in 5 Minutes?

missdrew:
HINT :

How many 8 Seconds are there in 5 Minutes?

Seems like you're suggesting me to create a looping.
But could you please tell me how?

I mean, which commands, how the looping would work?

Just a sample would help me. Thanks so much!

int timesMAX = 20; // 20 x 8 seconds
int currentCount = 0;

After wake increment currentCount
Check if it is == timesMAX
if it is
{
do stuff
reset the currentCount = 0
}
sleep again

Hint: study this excellent tutorial on low power operation.

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