Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mapping a range
#4
You mention 3 different ranges (0-3.3V, 0-4096bits, 0-100percent), but this algorithm will only map from one to a second. Which mapping do you want? I'm assuming you don't read volts at all but just get the bits from the ADC? I'd expect you'd want to map ADC to percent.

Your last code has both 3.3 and 4096 and 100. I'm not sure exactly what you're trying to convert from or to.

>>> def maprange( a, b, s):
...     (a1, a2), (b1, b2) = a, b
...     return  b1 + ((s - a1) * (b2 - b1) / (a2 - a1))
...
>>> maprange((0, 4096), (0, 100), 2048) #bits to percent
50.0
>>> maprange((0, 3.3), (0, 100), 1.5) #Volts to percent
45.45454545454546
Code is from the Rosetta Code project.
Reply


Messages In This Thread
Mapping a range - by ebolisa - Jun-12-2021, 07:41 PM
RE: Mapping a range - by deanhystad - Jun-12-2021, 07:46 PM
RE: Mapping a range - by ebolisa - Jun-12-2021, 08:29 PM
RE: Mapping a range - by bowlofred - Jun-12-2021, 09:56 PM
RE: Mapping a range - by deanhystad - Jun-12-2021, 10:33 PM
RE: Mapping a range - by ebolisa - Jun-12-2021, 11:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Mapping a value to an interval JazonKuer 12 2,120 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,844 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  matplotlib x axis range goes over the set range Pedroski55 5 3,291 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  mapping joystick axis to specific 'channels' in python DashOrion 1 2,656 Jul-07-2020, 04:26 PM
Last Post: DashOrion
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,179 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,584 Dec-10-2019, 10:34 PM
Last Post: fauveboyxuuki
  Maze Mapping with Image Processing furkankuyumcu 0 2,214 Dec-16-2018, 02:45 PM
Last Post: furkankuyumcu
  Python Idea Assist.: BerryIMU & Raspberry Pi 3 Audio Mapping StephLeber 0 2,313 Dec-09-2018, 10:43 AM
Last Post: StephLeber
  Python Inbuilt library for XAML to HTML5 mapping adityamandlekar 1 2,393 Jun-07-2018, 08:05 AM
Last Post: Larz60+
  Mapping Fields zak308 0 2,519 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020