Hello all,
I am trying to use watchdog timer with my code and i have some questions (please note that this is the first time that i work with watchdog timer so sorry if i ask stupid questions). My code is simple there are 3 light sensors and and LED and Buzzer. when the sensors detect light the LED and buzzer go on for a sec then the whole system shuts down for 4 sec. So i thought that i could use watchdog timer to make the the whole system to sleep during these 4 secs to save battery life.
After i tried coding the watchdog timer it seem to work but the problem is that it looks the the system looks for light and if there is not the system goes to sleep for 4 secs...then wakes up to look again then goes to sleep for 4secs again.
But what i want is when the system goes to sleep, after 4 secs it doesn't wake up again till it detects light again. (so after the LED and buzzer go off the system should go to sleep for 4 secs and after that it will stay in sleep mode till light is detected again)
(So sorry if my explaining is bad and thank you all in advance.)
Here is the code I'm using:
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
//int pinLed = 4;
int ledPin = 1; //PB3
int buzzPin = 0; //PB0
int sensorReading1 = 0;
int sensorReading2 = 0;
int sensorReading3 = 0;
int sensitvety = 60;
int turnOn = 0;
int sensor1 = 1; //PB5
int sensor2 = 2; //PB4
int sensor3 = 3; //PB2
volatile boolean f_wdt = 1;
void setup(){
//pinMode(pinLed,OUTPUT);
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
}
void loop(){
if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0; // reset flag
sensorReading1 = (analogRead(sensor1)/4);
sensorReading2 = (analogRead(sensor2)/4);
sensorReading3 = (analogRead(sensor3)/4);
if(sensorReading1 > sensitvety || sensorReading2 > sensitvety || sensorReading3 > sensitvety){
turnOn = 1;
}
if(turnOn){
delay(100);
digitalWrite(buzzPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(25);
digitalWrite(buzzPin, LOW);
delay(200);
digitalWrite(ledPin, LOW);
setup_watchdog(8); // approximately 4 seconds sleep
turnOn = 0;
}
//pinMode(pinLed,INPUT); // set all used port to intput to save power
pinMode(ledPin, INPUT);
pinMode(buzzPin, INPUT);
system_sleep();
//pinMode(pinLed,OUTPUT); // set all ports into state before sleep
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
}
}
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1; // set global flag
}