I need to generate a PWM output in proportion to a voltage signal on A0 input. The input will vary from 1 to 5V and this needs to be converted to a proportionate PWM out. I calculated as below :

Maximum Counts (A0) : 1024 = 5V

Hence each Volt = 1024 / 5 = 205 counts

1 to 5V will correspond to 205 to 1024 Counts

205 Counts = 0 PWMValue ( PWMV)

1024 Counts = 255 PWMV

Or each count = (255-0) / ( 1024-205) = 0.311 PWMV

**Transfer Function for PWMV = (Input Count – 205) x 0.311**

Finally the above calculation involves a int and float whereas the range for PWMV is a byte.

Can I cast the Input Count to a float() before the final calculation and then again cast the result PWMV to a byte() ? Will it work?