User Tools

Site Tools


en:pfw:linear-conversion

**This is an old revision of the document!**

A very simple method (no floating point, no algebraic formulas) for linear conversion of sensor output data to human-understandable units.

— Example 1, temperature —

Two points are given: Sensor output 400 and 1200 correspond to 10 and 36.3 degrees Celsius respectively.

To get rid of floating point, we calculate Celsius in tenths of degrees:

Sensor  Celsius
 400     100
1200     363
(800)   (263)  (Distance)

Take: SensorZ = the given sensor value closest to zero (400) CelsiusZ = the corresponding Celsius value (100) SensorDist = distance between the two sensor values (800) CelsiusDist = distance between the two Celsius values (263)

The conversion then looks like this:

: >CELSIUS    ( sensor -- celsius )
  SensorZ -   CelsiusDist SensorDist */   CelsiusZ + ;

The working forth definition is:

: >CELSIUS ( sensor -- celsius ) 400 - 263 800 */ 100 + ;
---------------------------------------------------------

NEGATE must be inserted after */ when increasing sensor values correspond to decreasing Celsius values.

— Example 2 —

Sensor Celsius

400     363

1200 100 (800) (263) (Distance)

: >CELSIUS ( sensor -- celsius ) 400 - 263 800 */ NEGATE 363 + ;
----------------------------------------------------------------

( an,jan2025)

en/pfw/linear-conversion.1739312406.txt.gz · Last modified: 2025-02-11 23:20 by albert