FORMULA TO CONVERT ADC VALUES (0-255) TO SERVO POSITIONS (75-225)
The need to convert the result of the ADC pins proportionally to servo positions arose recently in my Pic-Aim project, which in turn was inspired by this comment. In it, user chench shared the following formula to convert one into the other:
Readadc C.1,b1 ; read value into b1
b2 = (b1 / 2) + 75; convert from 0-255 to 75 - 203
Which works beautifully well.
However if you are looking for more accuracy -specially when refining and debugging code-, there is some room to improve on the given formula. I gave it to my younger sister who is a lot better at math than I am, and in 8 seconds flat she came up with the following:
- Y= is the servo position
- A (constant)=(225-75)/255 or 0.588235
- X= the ADC value
- B (also constant)= 75
A little implementation: Let´s say you want to calculate the servo position that would result from an ADC value of 111. All you have to do is replace the explained components with their values:
As you can see, the difference with Chench´s formula is minimal, but it is there.
Hope this helps any of you who finds themselves in the same situation I was in last week!
NOTE (July 28th, 2011): This formula will only work correctly with exponential potentiometers. If you are using linear ones, the servo response will not be full range.