Fix watchdog timer setup and power management

- Add missing watchdog header include
- Improve watchdog timer configuration with proper registers
- Switch to SLEEP_MODE_IDLE to maintain USB/Serial functionality
This commit is contained in:
2025-05-02 00:59:57 +02:00
committed by erickahmed
parent 9b20f33498
commit 7684470315
+13 -8
View File
@@ -1,4 +1,5 @@
#include <avr/sleep.h> #include <avr/sleep.h>
#include <avr/wdt.h>
#define MOISTURE_SENSOR_A0 A0 #define MOISTURE_SENSOR_A0 A0
@@ -8,16 +9,20 @@ ISR(WDT_vect) {
readSensors = true; readSensors = true;
} }
void watchdog() { void watchdogs() {
cli(); cli();
// Set watchdog timer to 8 seconds MCUSR &= ~(1 << WDRF);
WDTCSR = (1 << WDCE) | (1 << WDE); WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = (1 << WDIE) | (1 << WDP3); WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0);
sei(); sei();
} }
void sleep_mode() { void enterSleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Arduino disables USB when sleeping
// if using Serial, use flag : SLEEP_MODE_IDLE
// otherwise for deep sleep : SLEEP_MODE_PWR_DOWN
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable(); sleep_enable();
sleep_cpu(); sleep_cpu();
sleep_disable(); sleep_disable();
@@ -26,14 +31,14 @@ void sleep_mode() {
void setup() { void setup() {
pinMode(MOISTURE_SENSOR_A0, INPUT); pinMode(MOISTURE_SENSOR_A0, INPUT);
Serial.begin(9600); Serial.begin(9600);
watchdogs();
} }
void loop() { void loop() {
if (readSensors) { if (readSensors) {
uint16_t moisture = analogRead(MOISTURE_SENSOR_A0); uint16_t moisture = analogRead(MOISTURE_SENSOR_A0);
Serial.println(moisture); Serial.println(moisture);
readSensors = false; readSensors = false;
} }
sleep_mode(); enterSleep();
} }