Jun-12-2021, 09:56 PM
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.
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.45454545454546Code is from the Rosetta Code project.