Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <avr/sleep.h>
- #include <avr/wdt.h>
- #define LED 13
- // 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)
- {
- MCUSR = 0; // reset various flags
- WDTCSR |= 0b00011000; // see docs, set WDCE, WDE
- WDTCSR = 0b01000000 | interval; // set WDIE, and appropriate delay
- wdt_reset();
- byte adcsra_save = ADCSRA;
- byte prr_save = PRR;
- ADCSRA = 0; // disable ADC
- PRR = 0xFF; // turn off various modules
- set_sleep_mode (SLEEP_MODE_PWR_DOWN); // sleep mode is set here
- attachInterrupt (0, wake, FALLING); // allow grounding pin 2 to wake us
- sleep_enable();
- // turn off brown-out enable in software
- MCUCR = bit (BODS) | bit (BODSE);
- MCUCR = bit (BODS);
- sleep_mode (); // now goes to Sleep and waits for the interrupt
- detachInterrupt (0); // stop LOW interrupt
- //sleep_disable();
- ADCSRA = adcsra_save; // stop power reduction
- PRR = prr_save;
- } // end of myWatchdogEnable
- void blink ()
- {
- pinMode (LED, OUTPUT);
- digitalWrite (LED, HIGH);
- delay (1000);
- digitalWrite (LED, LOW);
- }
- void setup ()
- {
- digitalWrite (2, HIGH); // pull-up on button
- } // end of setup
- void loop()
- {
- blink();
- myWatchdogEnable (0b100001); // sleep for 8 seconds
- } // end of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement