#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#define LED 13
#define BUTTON 2
// interrupt service routine for when button pressed
void wake ()
{
wdt_disable(); // disable watchdog
} // end of wake
// watchdog interrupt
ISR (WDT_vect)
{
wake ();
} // end of WDT_vect
void myWatchdogEnable (const byte interval)
{
noInterrupts (); // timed sequence below
wdt_reset();
MCUSR = 0; // reset various flags
WDTCSR |= 0b00011000; // see docs, set WDCE, WDE
WDTCSR = 0b01000000 | interval; // set WDIE, and appropriate delay
byte adcsra_save = ADCSRA;
ADCSRA = 0; // disable ADC
power_all_disable(); // turn off all modules
set_sleep_mode (SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
attachInterrupt (digitalPinToInterrupt (BUTTON), wake, LOW); // allow grounding pin D2 to wake us
interrupts ();
sleep_cpu (); // now goes to Sleep and waits for the interrupt
detachInterrupt (digitalPinToInterrupt (BUTTON)); // stop LOW interrupt on pin D2
ADCSRA = adcsra_save; // stop power reduction
power_all_enable (); // turn on all modules
} // end of myWatchdogEnable
void setup ()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
} // end of setup
void loop()
{
digitalWrite (LED, HIGH);
delay (500);
digitalWrite (LED, LOW);
delay (500);
digitalWrite (LED,HIGH);
delay(500);
digitalWrite (LED,LOW);
delay(500);
digitalWrite (LED, HIGH);
delay(500);
// isikan bit ini kedalam parameter myWatchdogEnable untuk menentukan lama tunggu program sebelum reset
// 1 second: 0b000110
// 2 seconds: 0b000111
// 4 seconds: 0b100000
// 8 seconds: 0b100001
// sleep for 8 seconds
myWatchdogEnable (0b100001); // 8 seconds
} // end of loop