# 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=ax+b**

...where:

- 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:

y=(((225-75)/255)*111)+75

y=(0.588235*111)+75

y=65.294085+75

**y=140**

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!

Cheers,

Andrés

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.

## This explains the technique

This explains the technique I used perfectly. This was also explained somewhere in the picaxe manual 1 I believe(which is why I did it this way).

## Gotcha

Ok, birdmun. I get it. Thanks. I just didn´t know that decimals were called "floating points". I am fully aware of what kind of numbers the Picaxe "likes" and what kind of math they´ll accept. I guess I should have been more specific as to the theoretical nature of the tip. I thought that not giving any code samples was proof enough. I will rewrite it.

BTW, very cool that you know that not all of use use a decimal POINT, and that some of us, use a COMMA!!

Thanks for enlightening me, teach! ;)

Cheers,

Andrés

2