How to read Map() function in micropython?
How to read Map() function in micropython?
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?
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?
It's really just simple math.
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
>>>
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)
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?
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.
This is my generic mapping solution ... https://www.raspberrypi.org/forums/view ... 3#p1866943
That handles multiple mappings depending on input value.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to read Map() function in micropython?
Bear in mind that map is a Python keyword (with different functionality).
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: How to read Map() function in micropython?
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to read Map() function in micropython?
Indeed, it will work. But in my opinion it is not pretty and may confuse anyone reading the code.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to read Map() function in micropython?
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-
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?
we already did this last year: map function
for floats, a modification of the old code from Processing that Wiring/Arduino inherited will do:
For this reason, Arduino also provides the constrain() function if you want to keep the output range constrained
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))
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
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: How to read Map() function in micropython?
You can also read this.
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
Re: How to read Map() function in micropython?
This is very helpful, thanks a lot!