User Tools

Site Tools


mmbasic:map_function

Map() Function

Function maps a value in a given linear input range to it's proportional counterpart in the linear output range.

Often it is required to take a value in a specific range and “Map” it onto the equivalent value in some alternative range. e.g. a current measuring module might return a value in the range of 0-255 which corresponds to a current reading of 0-6Amps. The Map() function will take the measured value and the input/output ranges and return the equivalent value.

The input value is constrained so that it conforms to the input range, i.e. if the lower limit is zero and a value of negative 5 is supplied, the value of zero will be used. Likewise the upper limit.

Written to use Floats due to the likely nature of the application.

Syntax:

=Map(Value,InputRangeLowerLimit,InputRangeUpperLimit,OutputRangeLowerLimit,OutputRangeUpperLimit)

Example:

mAmps=Map(n,0,255,0,6000)'current in mA

	Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float
		Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo
	End Function

See also:

mmbasic/map_function.txt · Last modified: 2024/01/19 09:30 by 127.0.0.1