Hi, thanks for the messages in my in-box. I thought I would put my replies here in case anyone else wanted to chip in.
Using the following code, I get
"core.a(main.cpp.o): In function `main':
C:\arduino-1.0\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to `setup'"
Although there is nothing in setup, you still need to include it to avoid the error ie
void setup(){
}
Can you explain the math you used to set the time that the arduino sleeps in line " for (int
myLoop= 0; myLoop < 64; myLoop++) // loop around and give delay of 64 = 600 sec (10 mins)"?
Yes, sorry it incorrect maths. I was playing around with different loop iterations but didn't alter the
comment.
64 iterations of 8 seconds for the WDIE here:
// set interrupt mode and an interval
WDTCSR = _BV (WDIE) | _BV (WDP3) | _BV (WDP0); // set WDIE, and 8 seconds delay
64 x 8 = 512 seconds = 8.53 minutes. But you can easily change the loop iteration to suite your needs.
math for duration that it stays awake?
er...mmm.. As far as I understand it, the Watchdog timer goes to sleep for 8 seconds, wakes up for a split
second while the next iteration of the loop is executed, then goes back to sleep for the next 8 seconds,
and so on and on, until all the iterations have finished. Then the data log method is called.
ie this commented out code:
// LogData (); // now awake so log the data - this is my datalogging method call when avr is awake
it logs sensor data and call rtc etc.. then write to SD card
There may be a more efficient way of doing it but I couldn't think of it at the time.
if I upload the code into the arduino successfully and nothing happens because it entered a sleep
mode but has no wake up signal, can I just press the "reset" button in the arduino board to bring it back
to life?
Yes that will awake it but then it will go right back to sleep.
You will need to write an Interrupt Service Routine (ISR) that does something when the sleep is
interrupted.
So for example, write a test method that lights led's when LogData() method is called. Put LEDs on pins 14
and 15 (I used green and red ones) also put 470 to 560 ohm resistors in series so the led's don't blow.
and connect the cathodes to ground.
eg
#include <avr/sleep.h>
#include <avr/wdt.h>
//#include <SdFat.h> for SD card
#define ledPINgrn 14 // on when logging
#define ledPINred 15 // on when sleeping
byte old_ADCSRA;
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
}
void LogData ()//START of datalogger
METHODS***************************************************************************
{
// TO DO code here for datalogger
digitalWrite (ledPINgrn, HIGH); //led on when logging during testing
delay(5000); // delay 5 sec
} // end of Datalogger
methods***********************************************************************************
void setup()
{
// when testing using leds initialise outputs for leds
pinMode(ledPINred, OUTPUT);
pinMode(ledPINgrn, OUTPUT);
digitalWrite (ledPINred, LOW);
digitalWrite (ledPINgrn, LOW);
}
void loop ()
{
old_ADCSRA = ADCSRA; //save old status of ADC
//=============SLEEP ROUTINES========================================================================
for (int myLoop= 0; myLoop < 64; myLoop++) // SLEEP-LOOP loop around and give delay of 64 = 600 sec (10
mins)
{
digitalWrite (ledPINred, HIGH); //led on when sleep
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = _BV (WDCE) | _BV (WDE);
// set interrupt mode and an interval
WDTCSR = _BV (WDIE) | _BV (WDP3) | _BV (WDP0); // set WDIE, and 8 seconds delay
wdt_reset(); // pat the dog
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// turn off brown-out enable in software
MCUCR = _BV (BODS) | _BV (BODSE);
MCUCR = _BV (BODS);
sleep_cpu (); //nighty-night :)
}
// cancel sleep as a precaution
sleep_disable();
digitalWrite (ledPINred, LOW); //red led off when logging
//++++++++++++++++++++WAKE and CALL DATALOGGER++++++++++++++++++++++++++++++++++++++++
ADCSRA = old_ADCSRA; //enable ADC need to enable so ADC can get sensor data
LogData (); // now awake so log the data - this is my datalogging method call when avr is awake it logs
sensor data and call rtc etc.. then write to SD card
digitalWrite (ledPINgrn, LOW); //indicate finished logging
} // end
If it's working the red led should come on for 512 seconds (or however long you set the sleep-loop for)
and then go off. Then the green led should come on for 5 seconds [delay(5000)] as the LogData() method is
called. I suggest for this exercise setting the sleep-loop to a low number or else you will seem to be
waiting for ages.
cheers