ADCMin is the Minimum ADC Value we can have. ADCMax is the Maximum ADC Value we can have. Every routine must know it's limitations so first lets make sure we stay in range...
Code:
If ADC < ADCMin then ADC=ADCMin
If ADC > ADCMax then ADC=ADCMax
Now let's convert our ADC value... which varies between ADCMin (0%) and ADCMax (100%) but to an INVERSE Percentage
Code:
ADC=100-(((ADC-ADCMin)*100)/(ADCMax-ADCMin))
Our PWM varies between PWMMin (0%) and PWMMax(100%), so here let's calculate the PWMOut Value we need by Applying our previously calculated Percentage and not forgetting to add-in our minimum offset
Code:
PWMOut=(((PWMMax-PWMMin)*ADC)/100)+PWMMin
Four lines... and unless I've dropped a clanger - that should do the job.
Put some figures into the variables, pencil, paper and calculator, and see if it works for you...
Bookmarks