From 29b96288acbb58e66699f66f947e624afae55b0f Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 6 Oct 2021 01:46:21 -0400 Subject: use average of last 12 readings for photosensor --- mush.ino | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/mush.ino b/mush.ino index f517fb5..4400420 100644 --- a/mush.ino +++ b/mush.ino @@ -1,3 +1,5 @@ +#include +#include #include #include "ota.h" #include @@ -59,13 +61,27 @@ struct SensorState { float last_reading; virtual void sense() = 0; + std::list readings; + void record(float r) { + last_reading = r; + readings.push_back(r); + if (readings.size() > 12) + { + readings.pop_front(); + } + }; + float avg() + { + return std::accumulate(readings.begin(), readings.end(), 0.0) / readings.size(); + } + }; -struct TemperatureSensor : public SensorState { virtual void sense() { last_reading = dht.readTemperature(); } }; -struct HumiditySensor : public SensorState { virtual void sense() { last_reading = dht.readHumidity(); } }; -struct PhotoSensor : public SensorState { virtual void sense() { last_reading = analogRead(PHOTORESISTOR); } }; -struct CO2Sensor : public SensorState { virtual void sense() { last_reading = sensor.getCO2PPM(); } }; -struct TVOCSensor : public SensorState { virtual void sense() { last_reading = sensor.getTVOCPPB(); } }; +struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature()); } }; +struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; +struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; +struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; +struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; TemperatureSensor temperatureSensor; HumiditySensor humiditySensor; @@ -78,7 +94,7 @@ void log_reading() float temp_c = temperatureSensor.last_reading; float humidity = humiditySensor.last_reading; float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); - int photons = photoSensor.last_reading; + int photons = photoSensor.avg(); uint32_t co2 = cO2Sensor.last_reading; uint32_t tvoc = tVOCSensor.last_reading; @@ -107,7 +123,7 @@ void loop() auto ip = IPAddress(192,168,1,2); auto port = 3141; if (!wific->connected()) { - Serial.println("Attempting to connect"); + //Serial.println("Attempting to connect"); wific->connect(ip, port); } -- cgit v1.2.3