# From the ARDUINO world, MAP( ) command

In the world of Arduino, I can map values.

So if I have an input range of 1 - 20 I can map that to be 1 - 100.

So 1 = 1.
2 = 5
3 = 10
4 = 15
20 = 100

(I hope I did the intermediate values correctly)

Is there a way I can do it in `JS`?

I want to map an input of `1 - 10` to `153 - 500`

Take a look at the builtin Range node to see if it suits your needs
Iâ€™m using it to keep a 0-100% slider in my dashboard for brightness, but map it to/from 0-255 that the underlying lamp needs.

the range node will only do linear scaling... which may or may not be adequate.

With these values thatâ€™s a good point, I hadnâ€™t noticed the 153 part yet

Ah! `range`! Thanks.

Shall investigate.

If your numbers are none linear & it cant be done by an equation then a simple lookup might be the way to go...

``````var lookup = {
"1": 153,
...
"10": 500
}

var value = lookup[msg.payload]; // if payload is 10, you get 500
``````
1 Like

Thanks. The `range` seems to be working for now.

It is just for the temperature adjustment of a bulb.

The reason for the thought that you don't want linear is that your example shows the output changing by 4 as the input goes from 1 to 2, then by 5 as it goes from 2 to 3 and from 3 to 4, then by 85 for 4 to 20, which is 85/16 = 5.3 for each increment of the input. So the output is not a straight line.

I stuffed up the example.

1 - 20 remapped to 1 - 100 shouldn't be too hard.

I just didn't translate the 2, 3 and 4 values correctly.
But it is .....(thinking)
100/20 * `n` where `n` is the 1 - 20 value.

Yeah?

(But that's not really the issue. Sorry if I confused the issue.)

As said: `range` seems to be doing a good enough job.

It is just I don't want 100 positions on the scale. 20 should suffice for brightness adjustment.

I know (or just remembered) I can make it 1 - 100 and step value of 20 to get the same result.

1 Like

1 - 20 mapped to 1- 100 is 19 increments in to 99 out, 99/19 not 100/20. Perhaps you meant 0 - 20 mapped to 0 - 100?

Yeah. At this point we are splitting hairs. Sorry.

No. In IT the details are important to get right.

1 Like

Yes, I do agree with you @Colin.

My example was flawed. I used a vague thing to try and find the solution with the `range` node.

In the Arduino world the command is `map( )` (from memory).
I didn't have the knowledge to know it was called `range` in this one.

The example was only to establish what I was wanting to do.

yes - but we are all human - and we all talk about 0 - 100% - which is 101 values... when we mean 0 - 99.9999999 etc. and that is why I put in the "round to nearest integer" option in it, to help smooth out those bumps a bit - so you can map 0-100% to 0-255 on a bulb etc...

2 Likes

There's been lengthy discussions on the Arduino forum about the `map` function also as its use is not intuitive to many either.

1 Like

I guess there will always be a problem with which language's vocabulary is correct.

That isn't of contention for me. It was that I didn't equate `map( )` (arduino) to `range( )` (node-red)

I was told/shown. All good.

I figured that out . Just contributed on the discussion that mapping values is not as easy to get right as it first seems.

It isn't the number of unique integers that matters it is the number of intervals, which is 100. I disagree with the suggestion that we mean 0 - 99.99999. 99.99999 is less than 100. When you put 1 into a range node configured to map 0:1 to 1:100 you expect to get out exactly 100 if you put in exactly 1.
Similarly with the bulb example, 100% maps exactly to 255. The maths is (255-0)/(100-0) * input value, so 100 maps exactly to 255. No need for rounding, unless you don't want, for example, 1% to map the correct value which is 25.5.
There may be errors in the floating point calculations, but that is a different issue, best coped with by rounding when the value is displayed rather than when it is calculated.

2 Likes

Yeah, but I'm not as smart as you.

My language vocabulary is somewhat limited.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.