Hi everyone!
I am trying to get the "sleep & wake up" function for arduino to work for my outdoor project that uses a photocell to measure light levels, and I think it is getting "closer" to do what I would like it to do, but I'm kind of stuck, and hence, wanted to ask for advise & guidance from those of you out there...
I am hoping that this post can start a good discussion amongst fellow arduinoers, since there isn't that much clear information out there for beginners (like myself!!) on how one could put together a project like this.
In essence, what I would like my project to do is the following:
- arduino gets ready to go to sleep for 1st time
- shuts power off to photocell
- goes to sleep (sleep mode power down) for ~24 seconds
- after ~24 seconds, wakes up and powers up voltage for photocell
- begins sending light values to serial output for a duration of 10 seconds
- shuts power off to photocell
- goes to sleep for 24 seconds
- repeat starting at Step #4
I've been working with the "Watchdog Sleepy Bones" as my roadmap and slowly started to edit it, but I am a bit stuck on several of these steps and I'm not sure where & what I am doing wrong. The code runs, but I am not sure how to add the various steps that I would like it to do...
Therefore, any advise out there would be super appreciated!!
Here's the code I have so far (and slowly growing)...:
/*
Based on Watchdog Sleepy Bones sketch and very humbly being amended by Capo12...
Notes:
This is what a "mini sketch" for running a photocell would look like:
/////////////////////////////////////////////
// volt pin connected to arduino 5V
// ground pin connected to arduino ground
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.println(photocellReading); // the raw analog reading
}
/////////////////////////////////////////////
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
#define sleepTime 3 //number of 8 second sleep cycles (will sleep for ~24 seconds)
volatile byte wdt=0; //used to cound number of sleep cycles
void setup(){
setup_watchdog(); // set prescaller and enable interrupt
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here Power Down uses the least current
// system clock is turned off, so millis won't be reliable!
delay(10);
Serial.begin(9600); //serial baud rate 9600
//Arduino is just powering up...
}
void loop(){
// do what you want to before you sleep here
Serial.println("Ok, going to sleep now... :-)");
Serial.println("Step2");
system_sleep();
// do what you want to after you sleep here
Serial.println("Just waking up now...");
}
void system_sleep() {
ADCSRA |= (0<<ADEN); // disable ADC
sleep_enable(); // enable sleeping
while (wdt < sleepTime){ // sleep for sleepTime * 8sec
sleep_mode(); // activate system sleep
// sleeping ...
}
sleep_disable(); // disable sleep
ADCSRA |= (1<<ADEN); // switch ADC on
wdt = 0; // reset wdt for next cycle
}
void setup_watchdog() {
cli(); //disable global interrupts
MCUSR = 0x00; //clear all reset flags
//set WD_ChangeEnable and WD_resetEnable to alter the register
WDTCSR |= (1<<WDCE) | (1<<WDE); // this is a timed sequence to protect WDTCSR
// set new watchdog timeout value to 1024K cycles (~8.0 sec)
WDTCSR = (1<<WDP3) | (1<<WDP0);
//enable watchdog interrupt
WDTCSR |= (1<<WDIE);
sei(); //enable global interrupts
}
// Watchdog Interrupt Service Routine.
// Very first thing after sleep wakes with WDT Interrupt
ISR(WDT_vect) {
wdt++; // increment the watchdog timer
}