# new map function?

**URL:** <https://forum.arduino.cc/t/new-map-function/672298>\
**Category:** Programming\
**Created:** [August 29, 2020, 10:57am UTC](https://forum.arduino.cc/t/new-map-function/672298 "2020-08-29T10:57:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebasdt1](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sebasdt1](https://forum.arduino.cc/u/sebasdt1)\
**Post date:** [August 29, 2020, 10:57am UTC](https://forum.arduino.cc/t/new-map-function/672298/1 "2020-08-29T10:57:36Z")

</div>

For a bit of my code, I need a function that could display 2 decimal places in the serial monitor and uses a potentiometer.  
I have thought to do it with the map function, but that wouldn't work.  
That would only increase with 1.00 and not like 0.05 what I want.

What I have done is this:  
Use the map function to constrain it between 0 and 300 and then divide it by a 100, so I could get the value between 0.00 and 3.00.

here is the code that I used.  
I'm working with a esp-32.

```cpp
#define potentiometer 34 
#define TOLERANCE 5 
int oldVal;
float devVal;
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
pinMode(potentiometer, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  int val = analogRead(potentiometer);
  val = map(val, 0, 4095, 0, 300);

  delay(15);

  int diff = abs(val - oldVal);

  if (diff > TOLERANCE)
  {
    // lcd.backlight();
    oldVal = val;// only save if the val has changed enough to avoid slowly drifting
    devVal = val / 100; 

  }
  //Serial.print(val);
 // Serial.print(" ");
 // Serial.print(oldVal);
 // Serial.print(" ");
 // Serial.println(devVal);
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 29, 2020, 12:53pm UTC](https://forum.arduino.cc/t/new-map-function/672298/2 "2020-08-29T12:53:34Z")

</div>

instead of using integer maths for the map() function (that's how it's defined)

```cpp
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;
}

```

just write a new mapFloat() function where all the parameters are floating points. Then you won't get the issue with integer maths

```cpp
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 29, 2020, 2:02pm UTC](https://forum.arduino.cc/t/new-map-function/672298/3 "2020-08-29T14:02:18Z")

</div>

```cpp
  val = map(val, 0, 4095, 0, 300);

```

Range translation where the basis is zero, is not an appropriate use of map(). It's far simpler and clearer to simply do the math:

```cpp
val = val * 300.0 / 4096.0;

```

---

<div class="post-metadata">

**Author:** ![sebasdt1](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sebasdt1](https://forum.arduino.cc/u/sebasdt1)\
**Post date:** [August 29, 2020, 2:10pm UTC](https://forum.arduino.cc/t/new-map-function/672298/4 "2020-08-29T14:10:11Z")

</div>

Thank you for your reply and help!

> J-M-L:  
> just write a new mapFloat() function where all the parameters are floating points. Then you won't get the issue with integer maths
> 
> ```arduino
> float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
> 
> ```

{  
&nbsp; return (x - in\_min) \* (out\_max - out\_min) / (in\_max - in\_min) + out\_min;  
}

> ```arduino
> 
> ```

that's exactly what I needed.

> aarg:  
> Range translation where the basis is zero, is not an appropriate use of map(). It's far simpler and clearer to simply do the math:

i have tried this but the function only will return: 0.00

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 29, 2020, 2:13pm UTC](https://forum.arduino.cc/t/new-map-function/672298/5 "2020-08-29T14:13:56Z")

</div>

> sebasdt1:  
> i have tried this but the function only will return: 0.00

What function? You asked for a floating point solution, then you complain when it doesn't work with integer math...

Sheesh, then

```arduino
float result = val * 300.0 / 4096.0;

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 29, 2020, 2:15pm UTC](https://forum.arduino.cc/t/new-map-function/672298/6 "2020-08-29T14:15:18Z")

</div>

> that's exactly what I needed.

No, when `in_min` and `out_min` are 0, then just doing the math as aarg explains is faster (it's the same formula with the null members not being listed)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [August 29, 2020, 2:16pm UTC](https://forum.arduino.cc/t/new-map-function/672298/7 "2020-08-29T14:16:21Z")

</div>

fmap?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 29, 2020, 2:19pm UTC](https://forum.arduino.cc/t/new-map-function/672298/8 "2020-08-29T14:19:27Z")

</div>

> TheMemberFormerlyKnownAsAWOL:  
> fmap?

yes

---

<div class="post-metadata">

**Author:** ![sebasdt1](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sebasdt1](https://forum.arduino.cc/u/sebasdt1)\
**Post date:** [August 29, 2020, 2:30pm UTC](https://forum.arduino.cc/t/new-map-function/672298/9 "2020-08-29T14:30:45Z")

</div>

> aarg:  
> What function?

sorry I have misread your post.

> aarg:
> 
> ```arduino
> val = val * 300.0 / 4096.0;
> 
> ```

the mapFloat the function here above function just work fine

> TheMemberFormerlyKnownAsAWOL:  
> fmap?

when I put this in the ide it doesn't turn orange. is this function not implemented in the ide?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 29, 2020, 2:42pm UTC](https://forum.arduino.cc/t/new-map-function/672298/10 "2020-08-29T14:42:45Z")

</div>

just change the name mapFloat() to fmap() which is what it is usually called

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:43am UTC](https://forum.arduino.cc/t/new-map-function/672298/11 "2021-05-06T00:43:41Z")

</div>


