From 725df1f1ec20827ece34e7c7bf4dbb8f2e17fe14 Mon Sep 17 00:00:00 2001 From: Aditya Pulipaka Date: Thu, 20 Nov 2025 16:16:34 -0600 Subject: [PATCH] pre-averaging --- lib/IndSensorMap.cpp | 10 +++++----- lib/IndSensorMap.hpp | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/IndSensorMap.cpp b/lib/IndSensorMap.cpp index fdae3cd..e8080fd 100644 --- a/lib/IndSensorMap.cpp +++ b/lib/IndSensorMap.cpp @@ -6,22 +6,22 @@ IndSensorMap ind0Map = {-8.976076325826309, 913.5463710698101, 0.29767471011439534, 5.6686184386250025, 0.3627635461289861}; IndSensorMap ind1Map = {-4.831976283950702, 885.9877001844566, 0.2793284618109283, 3.8852507844119217, 0.2389935455347361}; IndSensorMap ind2Map = {-9.824360913609562, 871.4744633266955, 0.2909366235093304, 4.3307594408159495, 0.2822807132259202}; -IndSensorMap ind3Map = {-13.891292062248292, 990.6819962477331, 0.16376045588859353, -0.074904004740735, 0.17727132893449118}; +IndSensorMap ind3Map = {-13.8907146886418, 990.6824637304771, 0.16376005385006073, -0.07513804021312243, 0.1772655198934789}; // IndSensor class implementation IndSensor::IndSensor(IndSensorMap calibration, uint8_t analogPin) : consts(calibration), pin(analogPin), oor(false) {} // Convert raw analog reading to millimeters using sensor calibration -float IndSensor::toMM(unsigned int raw) { +float IndSensor::toMM(uint16_t raw) { return consts.C - (1.0 / consts.B) * log(pow((consts.K - consts.A) / ((float)raw - consts.A), consts.v) - 1.0); } // Read sensor directly from pin and convert to millimeters float IndSensor::readMM() { - unsigned int raw = analogRead(pin); - oor = (raw == 0 || raw > 870); // Update out-of-range flag - mmVal = toMM(raw); + analog = analogRead(pin); + oor = (analog == 0 || analog > 870); // Update out-of-range flag + mmVal = toMM(analog); return mmVal; } diff --git a/lib/IndSensorMap.hpp b/lib/IndSensorMap.hpp index 0705fd9..29815ce 100644 --- a/lib/IndSensorMap.hpp +++ b/lib/IndSensorMap.hpp @@ -16,6 +16,7 @@ class IndSensor { public: bool oor; float mmVal; + uint16_t analog; // Constructor IndSensor(IndSensorMap calibration, uint8_t analogPin); @@ -27,7 +28,7 @@ class IndSensor { uint8_t pin; // helper function to convert analog reading to millimeters - float toMM(unsigned int raw); + float toMM(uint16_t raw); }; // sensor instances