Hi guys, I am using the following code on the leonardo From this sitein the hopes to be able to have an interrupt based power saving arduino. The thing is I want to be able to run my code every 1/10th of a second, but the example code only explains how to change it by the second. Is it possible to change it, and also is it worth it considering my short time?
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/power.h>
const byte LED13 = 13;
byte ledStatus = 1;
volatile unsigned int counter = 0;
volatile byte flag = 0;
void setup() {
delay(10000);
MCUSR = 0; //reset the status register of the MCU
wdt_disable(); //disable the watchdog
pinMode(LED13, OUTPUT); //pin 13 in OUTPUT
digitalWrite(LED13, ledStatus); //LED on initially
setWdt(); //set up the watchdog
//set up the sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //sleep mode - POWER DOWN
}
void loop() {
power_all_disable();
sleep_mode(); //CPU in sleep-this corresponds to sleep_enable+sleep_cpu+sleep_disable
power_all_enable();
if (flag) {
wdt_disable();
counter = 0;
flag = 0;
//other things to do can be put here
ledStatus ^= 1; //toggle the LED
digitalWrite(LED13, ledStatus);
setWdt(); //re-set the watchdog
}
}
void setWdt() {
SREG &= ~(1<<SREG_I); //disable global interrupts
//prepare the watchdog's register
WDTCSR |= ((1<<WDCE) | (1<<WDE));
//set the "Interrupt Mode" with a timeout of 1 sec
WDTCSR = ((1<<WDIE)| (1<<WDP2) | (1<<WDP1));
SREG |= (1<<SREG_I); //re-enable global interrupts
}
ISR(WDT_vect) {
if (++counter >= 10) { //set here the # of seconds for the timeout
flag = 1;
}
}