Hi
I'm trying to learn to use interrupts in my projects, specifically I want to be able to use sleep mode in order to make battery powered projects. I have found an example by Nick Gammon (found on http://gammon.com.au/interrupts) where he blinks a diode and puts to arduino to sleep, waking it with the watchdog timer.
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.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)
{
noInterrupts (); // timed sequence below
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;
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 (0, wake, LOW); // allow grounding pin 2 to wake us
interrupts ();
sleep_cpu (); // now goes to Sleep and waits for the interrupt
detachInterrupt (0); // stop LOW interrupt
ADCSRA = adcsra_save; // stop power reduction
power_all_enable (); // turn on all modules
} // end of myWatchdogEnable
void setup ()
{
digitalWrite (2, HIGH); // pull-up on button
} // end of setup
void loop()
{
pinMode (LED, OUTPUT);
digitalWrite (LED, HIGH);
delay (5000);
digitalWrite (LED, LOW);
delay (5000);
// sleep bit patterns:
// 1 second: 0b000110
// 2 seconds: 0b000111
// 4 seconds: 0b100000
// 8 seconds: 0b100001
// sleep for 8 seconds
myWatchdogEnable (0b100001); // 8 seconds
} // end of loop
This code works fine, but I tried to add a serial.print in the main loop and it doesn't work, I also tried to write to an SD-card, which did not work either, can anyone explain why this doesn't work?
I also tried to add serial.print inside the myWatchdogEnable() function to help me understand the bitwise operations. Here the Serial.print() worked, which I don't really understand either because Serial.print() needs interruptes enabled and the first line in myWatchdogEnable() disables interrupts.
I hope someone will help me understand what I am doing wrong and help me correct my mistakes.