Adc Value To Servo Movement

 
#1

Hi all. I am trying to get my ADC values to move a normal servo. In this project a potentiometer will move a normal servo as in if at 2.5v the servo is centre. If 0v it will move to 1 if 5v it will move to 50 and so on. Please bare in mind I do hard ware, coding even in this simple way is tough for me.

Thanks all.

Matt

#2

ADC is from 0 to 255
0v = ADC of 0, 5v = ADC of 255

So we work out the positions of the servo, nice and easy, 100 positions so an ADC of 2.55 for each position. Position 50 would be ADC of 128, position 25 would be ADC of 56... you get the idea.

First you need to get the ADC Value, I've assumed it's on ADC0 but change ADC0 to whichever you have it on;

Code:

$value=GetADC(ADC0)



Then you convert that to the servo position;

Code:

$position = $value/100



Since it needs to be an integer we round it off to 0 decimal places;

Code:

$position = Round($position,0)



And we move the servo - assumed D1 but again, change D1 to suit;

Code:

Servo(D1,$position)



And voila, it's done. Loop it with a sleep to save on processing to continually monitor and move... the script should look something like this (note: Done from memory and untested, post any issues I'll relook when I'm home).

Code:


# ADC to Servo movement
:loop
$value=GetADC(ADC0)
$position = $value/100
$position = Round($position,0)
Servo(D1,$position)
Sleep(250)
Goto(loop)

#3

Thank you so much that is exactly what I need, thank you very much Smile