Page 1 of 2

How to read Map() function in micropython?

Posted: Fri May 28, 2021 6:57 am
by wael
I'm new to micropython.
How to write Map() function in micropython?
In arduino C:
int mappedValue;
int moistureValue = analogRead(A0);
mappedValue = map(moistureValue,AirValue,WaterValue, 0, 100);
How toconvert it into micropython?

How to use the library? I use Thonny but where I can see the examples Arduino ide?

Re: How to read Map() function in micropython?

Posted: Fri May 28, 2021 10:02 am
by fe2o3
It's really just simple math.

Code: Select all

def map(val, loval, hival, tolow, tohigh):
     if loval <= val <= hival:
         return (val - loval)/(hival-loval)*(tohigh-tolow) + tolow
     else:
         raise(ValueError)
You will NEED to verify that (loval < hival) and that (tolow < tohigh)
and return the result in the form you want (int vs float).

A Celsius to Fahrenheit conversion would be as follows:
>>> map(37, 0, 100, 32, 212)
98.6
>>>

Re: How to read Map() function in micropython?

Posted: Fri May 28, 2021 2:25 pm
by hippy
For a simple proportional mapping a single function as given by 'fe203' will suffice.

This is my generic mapping solution ... https://www.raspberrypi.org/forums/view ... 3#p1866943

That handles multiple mappings depending on input value.

Re: How to read Map() function in micropython?

Posted: Fri May 28, 2021 5:26 pm
by pythoncoder
Bear in mind that map is a Python keyword (with different functionality).

Re: How to read Map() function in micropython?

Posted: Sun May 30, 2021 8:26 pm
by SpotlightKid
It's actually a built-in function (or rather a class acting like a function), which means you can use "map" for identifiers without getting a syntax error. You will then of course shadow the builtin function.

Re: How to read Map() function in micropython?

Posted: Mon May 31, 2021 5:28 am
by pythoncoder
Indeed, it will work. But in my opinion it is not pretty and may confuse anyone reading the code.

Re: How to read Map() function in micropython?

Posted: Mon May 31, 2021 8:38 am
by fe2o3
I'd probably just rename map() to rescale(). The map(), as described
by the op, reminds me of the map function from Parallax BASIC for
their Stamp controllers.

-Rusty-

Re: How to read Map() function in micropython?

Posted: Mon May 31, 2021 7:20 pm
by scruss
we already did this last year: map function

for floats, a modification of the old code from Processing that Wiring/Arduino inherited will do:

Code: Select all

def valmap(value, istart, istop, ostart, ostop):
  return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
fe2o3 wrote:
Fri May 28, 2021 10:02 am
You will NEED to verify that (loval < hival) and that (tolow < tohigh)
Not if you're extrapolating, which is often more useful than stopping with an exception.
For this reason, Arduino also provides the constrain() function if you want to keep the output range constrained

Re: How to read Map() function in micropython?

Posted: Tue Jun 01, 2021 4:55 am
by OlivierLenoir
You can also read this.

Re: How to read Map() function in micropython?

Posted: Tue Jun 01, 2021 7:50 am
by Jackli
OlivierLenoir wrote:
Tue Jun 01, 2021 4:55 am
You can also read this.
This is very helpful, thanks a lot!