# From the ARDUINO world, MAP( ) command

**URL:** <https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072>\
**Category:** General\
**Created:** [22 May 2020 08:18 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072 "2020-05-22T08:18:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:18 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/1 "2020-05-22T08:18:23Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [22 May 2020 08:21 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/2 "2020-05-22T08:21:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 May 2020 08:23 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/3 "2020-05-22T08:23:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [22 May 2020 08:24 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/4 "2020-05-22T08:24:46Z")

</div>

With these values that’s a good point, I hadn’t noticed the 153 part yet 🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:25 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/5 "2020-05-22T08:25:00Z")

</div>

Ah! `range`! Thanks.

Shall investigate.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 May 2020 08:31 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/6 "2020-05-22T08:31:35Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:33 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/7 "2020-05-22T08:33:38Z")

</div>

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

It is just for the _temperature_ adjustment of a bulb.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 May 2020 08:34 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/8 "2020-05-22T08:34:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:37 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/9 "2020-05-22T08:37:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 May 2020 08:40 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/10 "2020-05-22T08:40:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:40 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/11 "2020-05-22T08:40:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 May 2020 08:45 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/12 "2020-05-22T08:45:47Z")

</div>

> [@Trying\_to\_learn](#):
>
> At this point we are splitting hairs

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:48 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/13 "2020-05-22T08:48:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 May 2020 08:49 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/14 "2020-05-22T08:49:39Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [22 May 2020 08:57 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/15 "2020-05-22T08:57:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 08:59 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/16 "2020-05-22T08:59:56Z")

</div>

> [@ristomatti](#):
>
> There's been lengthy discussions on the Arduino forum about the `map` function also as it's use is not intuitive to many either.

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.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [22 May 2020 09:07 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/17 "2020-05-22T09:07:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 May 2020 09:11 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/18 "2020-05-22T09:11:47Z")

</div>

> [@dceejay](#):
>
> we all talk about 0 - 100% - which is 101 values.. when we mean 0 - 99.9999999

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 May 2020 09:11 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/19 "2020-05-22T09:11:55Z")

</div>

> [@ristomatti](#):
>
> I figured that out

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

My _language_ vocabulary is somewhat limited.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 June 2020 09:11 UTC](https://discourse.nodered.org/t/from-the-arduino-world-map-command/27072/20 "2020-06-05T09:11:57Z")

</div>

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