Hello, I am trying to build an arduino sprinkler timer.
For this application I will need the Arduino to be completely idle for many hours and consume little electricity.
My current test code i believe switches into SLEEP_MODE_PWR_DOWN for 8 seconds.. which is the maximum sleep time? it then increments a counter.
once it reaches 225 8 second intervals (30 minutes)
it actuates the servo.
I tested this setup and it ran for only about 20 hours on a single 9v battery.
so it idled for 20 hours and switched the servo 40 times.
Is this this maximum idle power efficiency I can achieve?
// Sweep
// by BARRAGAN
#include <Servo.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
int pos = 0;
volatile int f_wdt=1;
int longCounter = 0;
//SLEEP RELATED CODE
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
}
else
{
Serial.println("WDT Overrun!!!");
}
}
void enterSleep(void)
{
//SLEEP_MODE_PWR_SAVE
set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
//
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
//Sleep Related Code
//Serial.begin(9600);
//Serial.println("Initialising...");
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
// Serial.println("Initialisation complete.");
}
void loop()
{
longCounter ++;
if (longCounter > 225) {
longCounter = 0;
if (pos == 0) {
pos = 180;
} else {
pos = 0;
}
myservo.write(pos);
delay(1000);
}
f_wdt = 0;
enterSleep();
}