Implement multiple sensors reading for moisture

This commit is contained in:
2025-05-14 23:41:06 +02:00
parent 1a10abc4d6
commit fe2e04ee38
4 changed files with 58 additions and 36 deletions
+3 -2
View File
@@ -9,10 +9,11 @@
#define MOISTURE_SENSOR_A5 PC5 #define MOISTURE_SENSOR_A5 PC5
// NOTE: PC6 PC7 are not accessible by default on Arduino Uno R3 // NOTE: PC6 PC7 are not accessible by default on Arduino Uno R3
// You need to use the ATMega328p on a custom PCB and use pins 1 and 2 // You need to use the ATMega328p on a custom PCB and use pins 1 and 2
#define MOISTURE_SENSOR_A6 PC6 //#define MOISTURE_SENSOR_A6 PC6
#define MOISTURE_SENSOR_A7 PC7 //#define MOISTURE_SENSOR_A7 PC7
#define SENSOR_READINGS 5 #define SENSOR_READINGS 5
#define SENSORS_NUM 6
#endif #endif
+6 -7
View File
@@ -4,9 +4,10 @@
#include <avr/wdt.h> #include <avr/wdt.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "moisture.h" #include "moisture.h"
#include "adc.h"
ISR(WDT_vect) { ISR(WDT_vect) {
readSensors = true; triggerSensorsRead();
} }
void watchdogs(void) { void watchdogs(void) {
@@ -25,7 +26,8 @@ void enterSleep(void) {
} }
void setup(void) { void setup(void) {
initMoistureSensor(); adcInit();
initMoistureSensors();
watchdogs(); watchdogs();
} }
@@ -33,13 +35,10 @@ int main(void) {
setup(); setup();
while(true) { while(true) {
if (readSensors) { if (readSensors) {
int16_t result = readMoisture(); int16_t result = readMoisture();
if (result != -1) {
//Serial.println(result); //TODO: send result to esp32
readSensors = false;
}
} }
enterSleep(); enterSleep();
} }
+46 -25
View File
@@ -6,36 +6,57 @@
#include "config.h" #include "config.h"
#include "adc.h" #include "adc.h"
static int16_t moistureReadingSum = 0;
static int8_t moistureReadingCount = 0;
volatile bool readSensors = false; volatile bool readSensors = false;
void initMoistureSensor(void) { void initMoistureSensors(void) {
DDRC &= ~(1 << MOISTURE_SENSOR_A0); // set as input // Set as input
PORTC &= ~(1 << MOISTURE_SENSOR_A0); //disable pullup DDRC &= ~((1 << MOISTURE_SENSOR_A0) |
(1 << MOISTURE_SENSOR_A1) |
(1 << MOISTURE_SENSOR_A2) |
(1 << MOISTURE_SENSOR_A3) |
(1 << MOISTURE_SENSOR_A4) |
(1 << MOISTURE_SENSOR_A5)) ;
//(1 << MOISTURE_SENSOR_A6) |
//(1 << MOISTURE_SENSOR_A7));
// Disable pullup
PORTC &= ~((1 << MOISTURE_SENSOR_A0) |
(1 << MOISTURE_SENSOR_A1) |
(1 << MOISTURE_SENSOR_A2) |
(1 << MOISTURE_SENSOR_A3) |
(1 << MOISTURE_SENSOR_A4) |
(1 << MOISTURE_SENSOR_A5)) ;
//(1 << MOISTURE_SENSOR_A6) |
//(1 << MOISTURE_SENSOR_A7));
} }
void triggerSensorRead(void) { void triggerSensorsRead(void) {
readSensors = true; readSensors = true;
} }
int16_t readMoisture(void) { int16_t moistureSensorsAverage(int8_t sensorPin) {
if (!readSensors) int16_t sum = 0;
return -1; for (int i = 0; i < SENSOR_READINGS; i++) {
sum += adcRead(sensorPin);
adcInit(); //TODO: implement variance check (if over a certain variance discard measurements for this cycle)
moistureReadingSum += adcRead(MOISTURE_SENSOR_A0); //TODO: maybe add a small delay using watchdog here?
moistureReadingCount++; //TODO IMPORTANT: if value < 1 or > 1022, discard that measurement
}
//TODO: implement variance check (if over a certain variance discard measurements for this cicle) return sum / SENSOR_READINGS;
if (moistureReadingCount >= SENSOR_READINGS) { }
int16_t average = moistureReadingSum / SENSOR_READINGS;
moistureReadingSum = 0; int16_t readMoisture(void) {
moistureReadingCount = 0; uint8_t sensorPins[SENSORS_NUM] = { MOISTURE_SENSOR_A0, MOISTURE_SENSOR_A1,
readSensors = false; MOISTURE_SENSOR_A2, MOISTURE_SENSOR_A3,
return average; MOISTURE_SENSOR_A4, MOISTURE_SENSOR_A5 };
} else readSensors = true; //MOISTURE_SENSOR_A6, MOISTURE_SENSOR_A7 };
return -1; int16_t overallSum = 0;
for (int i = 0; i < SENSORS_NUM; i++) {
int16_t sensorAverage = moistureSensorsAverage(sensorPins[i]);
//TODO: if a sensor gives 0 or 1023, discard that sensor entirely (means it is disconnected or not working)
overallSum += sensorAverage;
}
return overallSum / SENSORS_NUM;
} }
+3 -2
View File
@@ -4,9 +4,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
void initMoistureSensor(void); void initMoistureSensors(void);
void triggerSensorsRead(void);
int16_t moistureSensorsAverage(int8_t sensorPin);
int16_t readMoisture(void); int16_t readMoisture(void);
void triggerSensorRead(void);
extern volatile bool readSensors; extern volatile bool readSensors;