At line (3), we have a function named map_value():
def map_value(in_v, in_min, in_max, out_min, out_max): # (3)
"""Helper method to map an input value (v_in)
between alternative max/min ranges."""
v = (in_v - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
if v < out_min: v = out_min elif v > out_max: v = out_max
return v
The purpose of this method is to map an input range of values into another range of values. For example, we use this function to map the analog input voltage range 0-3.3 volts into a frequency range 0-60. You will frequently use a value-mapping function like this when working with analog inputs to map raw analog input values into more meaningful values for your code.
Next, we are ready to create the PWM signal.