I have this code I found on the net and Adafruits sleepydog. Just curious if I am missing something. is there a way of resuming other code after sleeping? As in wake up turn serial back on and stop going back into sleep mode? If so would you mind showing me with the AnalogReadSerial sketch? Thanks.
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
volatile int f_wdt = 1;
/***************************************************
Name: ISR(WDT_vect)
Returns: Nothing.
Parameters: None.
Description: Watchdog Interrupt Service. This
is executed when watchdog timed out.
***************************************************/
ISR(WDT_vect)
{
if (f_wdt == 0)
{
f_wdt = 1;
}
else
{
Serial.println("WDT Overrun!!!");
}
}
/***************************************************
Name: enterSleep
Returns: Nothing.
Parameters: None.
Description: Enters the arduino into sleep mode.
***************************************************/
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN ..SLEEP_MODE_PWR_SAVEfor 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();
}
/***************************************************
Name: setup
Returns: Nothing.
Parameters: None.
Description: Setup for the serial comms and the
Watch dog timeout.
***************************************************/
#define ledPin 13
#define VBATPIN A9
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.println("Initialising...");
delay(100); //Allow for serial print to complete.
/*** 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.");
delay(100); //Allow for serial print to complete.
pinMode(ledPin, OUTPUT);
//pinMode(LED, OUTPUT);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
//while (!Serial);
Serial.begin(9600);
delay(100);
}
void VBATPINcheck() // read pins
{
float measuredvbat = analogRead(VBATPIN);
int VBATPINsense = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
Serial.print("VBat: " ); Serial.print(measuredvbat); Serial.print("VBatPIN: " ); Serial.println(VBATPINsense);
}void loop()
{ if (f_wdt == 1)
{
/* Toggle the LED */
digitalWrite(ledPin, !digitalRead(ledPin));
/* Don't forget to clear the flag. */
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
}
else
{
/* Do nothing. */
}
[code]