Add Makefile and refactor moisture reading to separate module

This commit is contained in:
2025-05-02 14:40:58 +02:00
committed by erickahmed
parent 4486febccf
commit bf7d177046
4 changed files with 103 additions and 31 deletions
+12 -31
View File
@@ -1,19 +1,14 @@
#include <stdint.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#define MOISTURE_SENSOR_A0 A0
volatile bool readSensors = false;
int16_t moistureReadingSum = 0;
int8_t moistureReadingCount = 0;
const uint8_t SENSOR_READINGS = 3;
#include "config.h"
#include "moisture.h"
ISR(WDT_vect) {
readSensors = true;
}
void watchdogs() {
void watchdogs(void) {
cli();
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1 << WDCE) | (1 << WDE);
@@ -21,38 +16,24 @@ void watchdogs() {
sei();
}
int16_t readMoisture() {
if (readSensors) {
moistureReadingSum += analogRead(MOISTURE_SENSOR_A0);
moistureReadingCount++;
if (moistureReadingCount >= SENSOR_READINGS) {
Serial.println(moistureReadingSum / SENSOR_READINGS);
moistureReadingSum = 0;
moistureReadingCount = 0;
} else readSensors = true;
}
readSensors = false;
}
void enterSleep() {
// Arduino disables USB when sleeping
// if using Serial, use flag : SLEEP_MODE_IDLE
// otherwise for deep sleep : SLEEP_MODE_PWR_DOWN
void enterSleep(void) {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_cpu();
sleep_disable();
}
void setup() {
pinMode(MOISTURE_SENSOR_A0, INPUT);
void setup(void) {
Serial.begin(9600);
initMoistureSensor();
watchdogs();
}
void loop() {
int16_t moisture = readMoisture();
void loop(void) {
triggerSensorRead();
int16_t result = readMoisture();
if (result != -1) {
}
enterSleep();
}