Life would be easier with the right function

I noticed a C++ function using the Arduino Desktop IDE that would help me with range conversions. There may be a better term for it but what I’m looking for is a function I can send an input range, an output range, and a value to convert. In my case, 0 to .6 should yield an output in the range of 1 to 0. Yes, a backward direction in this case. I’m not sure what the equation is yet. Hopefully, someone might already have this function in their toolbox. If not, even the equation would help.

I think you mean the map function? The arduino docs have an example implementation. Scroll to the appendix section: map() - Arduino Reference

In Xojo it would be something like this:

function map (value as double, fromLow as double, fromHigh as double, toLow as double, toHigh as double) as double
  if value <= fromLow then return toLow
  if value >= fromHigh then return toHigh
  dim portion as double = (value - fromLow) / (fromHigh - fromLow)
  return toLow + (toHigh - toLow)*portion
end function

(I wrote that from memory, I think it’s right?)

Edit to add - hmm, I think this version will fail if the low and high values are reversed as in the example you gave.

What happens if the value is outside the range given? Does the value get stuck at the low or high range or can it go outside it?

An output of no less than 0 but it will not create an error. Maybe I need to somehow invert the input value first before sending to the function. .6 would = 0 and 0 would = .6 Must be a way to do that.

The Arduino map code:

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Maybe effectively the same.

Some differences:

  • The Arduino code is using integers, which is probably not what is desired
  • The Arduino code does not keep the output wihin the range, e.g. if x is less than in_min, the output will be less than out_min

On second thought, I think my code will work assuming fromLow < fromHigh, even if toLow > toHigh . It won’t work however if fromLow > fromHigh.

With a small change to the Map function out of range code this works great. Thanks for the help on this.

LightFactor = LT_Value * map(CloudFactor,1,.3,1,0)

function map (value as double, fromLow as double, fromHigh as double, toLow as double, toHigh as double) as double
if value >= fromLow then return toLow
if value <= fromHigh then return toHigh
dim portion as double = (value - fromLow) / (fromHigh - fromLow)
return toLow + (toHigh - toLow)*portion
end function

What actually worked after I tested all input values was the Arduino code mentioned and setting all limits to doubles, should anyone need such the function.

LightFactor = map(CloudFactor,1,.3,0,1)

function map (x as double, in_min as double, in_max as double, out_max as double, out_max as double) as double

If x<in_max Then x = in_max
Return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.