Skip to content

Commit ea0a8ef

Browse files
Copilotdorkmo
andcommitted
Extract unit conversion helper functions and document blind spot limitation
- Add getPressureConversionFactor() helper function for PSI/bar/kPa/mbar/inH2O conversions - Add getDistanceConversionFactor() helper function for m/cm/ft/in conversions - Refactor 3 places with duplicated pressure conversion logic to use helper - Refactor ultrasonic distance conversion to use helper - Document sensor blind spot limitation in README (sensors cannot detect liquid below mount height) Co-authored-by: dorkmo <[email protected]>
1 parent 91d0eca commit ea0a8ef

File tree

2 files changed

+24
-44
lines changed

2 files changed

+24
-44
lines changed

TankAlarm-112025-Client-BluesOpta/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Used for sensors like the Dwyer 626-06-CB-P1-E5-S1 (0-5 PSI) mounted near the bo
102102
- `sensorRangeUnit`: Pressure unit - "PSI", "bar", "kPa", "mbar", or "inH2O"
103103
- **Sensor Mount Height**: Height of sensor above tank bottom (usually 0-2 inches)
104104

105+
**Known Limitation - Blind Spot:** Pressure sensors cannot detect liquid levels below their mount height. When the tank is empty (0 PSI), the reported level will be the sensor mount height (e.g., 2"), not 0". Mount the sensor as close to the tank bottom as possible to minimize this blind spot.
106+
105107
**Pressure-to-Height Conversion:**
106108
The system automatically converts pressure to inches using these factors:
107109
- 1 PSI = 27.68 inches of water
@@ -122,7 +124,7 @@ The system automatically converts pressure to inches using these factors:
122124

123125
**How It Works:**
124126
1. 4mA → 0 PSI → 0 inches of liquid above sensor
125-
2. Total height = 0 + 2" mount height = 2" (sensor position, not liquid)
127+
2. Total height = 0 + 2" mount height = 2" (minimum reported value due to blind spot)
126128
3. When tank fills: 12mA → 2.5 PSI → 69.2" + 2" = 71.2" total
127129

128130
#### Ultrasonic Sensor (Top-Mounted)

TankAlarm-112025-Client-BluesOpta/TankAlarm-112025-Client-BluesOpta.ino

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ static size_t strlcpy(char *dst, const char *src, size_t size) {
230230
#define MBAR_TO_INCHES_WATER 0.4015f // 1 mbar = 0.4015 inches of water
231231
#endif
232232

233+
// Helper function: Get pressure-to-inches conversion factor based on unit
234+
static float getPressureConversionFactor(const char* unit) {
235+
if (strcmp(unit, "bar") == 0) return BAR_TO_INCHES_WATER;
236+
if (strcmp(unit, "kPa") == 0) return KPA_TO_INCHES_WATER;
237+
if (strcmp(unit, "mbar") == 0) return MBAR_TO_INCHES_WATER;
238+
if (strcmp(unit, "inH2O") == 0) return 1.0f;
239+
return PSI_TO_INCHES_WATER; // Default: PSI
240+
}
241+
242+
// Helper function: Get distance-to-inches conversion factor based on unit
243+
static float getDistanceConversionFactor(const char* unit) {
244+
if (strcmp(unit, "m") == 0) return METERS_TO_INCHES;
245+
if (strcmp(unit, "cm") == 0) return CENTIMETERS_TO_INCHES;
246+
if (strcmp(unit, "ft") == 0) return FEET_TO_INCHES;
247+
return 1.0f; // Default: assume inches
248+
}
249+
233250
static const uint8_t NOTECARD_I2C_ADDRESS = 0x17;
234251
static const uint32_t NOTECARD_I2C_FREQUENCY = 400000UL;
235252

@@ -1474,21 +1491,12 @@ static bool validateSensorReading(uint8_t idx, float reading) {
14741491

14751492
if ((isCurrentLoop || isAnalogWithVoltageRange) && hasNativeRange) {
14761493
// For sensors with native range, calculate max from sensor range
1477-
float conversionFactor = PSI_TO_INCHES_WATER;
14781494
if (isCurrentLoop && cfg.currentLoopType == CURRENT_LOOP_ULTRASONIC) {
14791495
// Ultrasonic: max level is sensorMountHeight (when tank is full)
14801496
maxValid = cfg.sensorMountHeight * 1.1f;
14811497
} else {
14821498
// Pressure: calculate max from pressure range
1483-
if (strcmp(cfg.sensorRangeUnit, "bar") == 0) {
1484-
conversionFactor = BAR_TO_INCHES_WATER;
1485-
} else if (strcmp(cfg.sensorRangeUnit, "kPa") == 0) {
1486-
conversionFactor = KPA_TO_INCHES_WATER;
1487-
} else if (strcmp(cfg.sensorRangeUnit, "mbar") == 0) {
1488-
conversionFactor = MBAR_TO_INCHES_WATER;
1489-
} else if (strcmp(cfg.sensorRangeUnit, "inH2O") == 0) {
1490-
conversionFactor = 1.0f;
1491-
}
1499+
float conversionFactor = getPressureConversionFactor(cfg.sensorRangeUnit);
14921500
maxValid = (cfg.sensorRangeMax * conversionFactor + cfg.sensorMountHeight) * 1.1f;
14931501
}
14941502
minValid = -maxValid * 0.1f;
@@ -1642,18 +1650,7 @@ static float readTankSensor(uint8_t idx) {
16421650
cfg.sensorRangeMin, cfg.sensorRangeMax);
16431651

16441652
// Convert pressure to liquid height in inches using appropriate conversion factor
1645-
float conversionFactor = PSI_TO_INCHES_WATER; // Default: PSI
1646-
if (strcmp(cfg.sensorRangeUnit, "bar") == 0) {
1647-
conversionFactor = BAR_TO_INCHES_WATER;
1648-
} else if (strcmp(cfg.sensorRangeUnit, "kPa") == 0) {
1649-
conversionFactor = KPA_TO_INCHES_WATER;
1650-
} else if (strcmp(cfg.sensorRangeUnit, "mbar") == 0) {
1651-
conversionFactor = MBAR_TO_INCHES_WATER;
1652-
} else if (strcmp(cfg.sensorRangeUnit, "inH2O") == 0) {
1653-
conversionFactor = 1.0f; // Already in inches of water
1654-
}
1655-
// else assume PSI
1656-
1653+
float conversionFactor = getPressureConversionFactor(cfg.sensorRangeUnit);
16571654
float liquidAboveSensor = pressure * conversionFactor;
16581655

16591656
// Total height from tank bottom = liquid above sensor + sensor mount height
@@ -1693,15 +1690,7 @@ static float readTankSensor(uint8_t idx) {
16931690
cfg.sensorRangeMin, cfg.sensorRangeMax);
16941691

16951692
// Convert distance to inches based on sensorRangeUnit
1696-
float distanceInches = distanceNative;
1697-
if (strcmp(cfg.sensorRangeUnit, "m") == 0) {
1698-
distanceInches = distanceNative * METERS_TO_INCHES;
1699-
} else if (strcmp(cfg.sensorRangeUnit, "cm") == 0) {
1700-
distanceInches = distanceNative * CENTIMETERS_TO_INCHES;
1701-
} else if (strcmp(cfg.sensorRangeUnit, "ft") == 0) {
1702-
distanceInches = distanceNative * FEET_TO_INCHES;
1703-
}
1704-
// else assume already in inches
1693+
float distanceInches = distanceNative * getDistanceConversionFactor(cfg.sensorRangeUnit);
17051694

17061695
// Calculate liquid level: tank height - distance from sensor to surface
17071696
levelInches = cfg.sensorMountHeight - distanceInches;
@@ -1719,18 +1708,7 @@ static float readTankSensor(uint8_t idx) {
17191708
cfg.sensorRangeMin, cfg.sensorRangeMax);
17201709

17211710
// Convert pressure to liquid height in inches using appropriate conversion factor
1722-
float conversionFactor = PSI_TO_INCHES_WATER; // Default: PSI
1723-
if (strcmp(cfg.sensorRangeUnit, "bar") == 0) {
1724-
conversionFactor = BAR_TO_INCHES_WATER;
1725-
} else if (strcmp(cfg.sensorRangeUnit, "kPa") == 0) {
1726-
conversionFactor = KPA_TO_INCHES_WATER;
1727-
} else if (strcmp(cfg.sensorRangeUnit, "mbar") == 0) {
1728-
conversionFactor = MBAR_TO_INCHES_WATER;
1729-
} else if (strcmp(cfg.sensorRangeUnit, "inH2O") == 0) {
1730-
conversionFactor = 1.0f; // Already in inches of water
1731-
}
1732-
// else assume PSI
1733-
1711+
float conversionFactor = getPressureConversionFactor(cfg.sensorRangeUnit);
17341712
float liquidAboveSensor = pressure * conversionFactor;
17351713

17361714
// Total height from tank bottom = liquid above sensor + sensor mount height

0 commit comments

Comments
 (0)