United Kingdom
Asked — Edited

Adc Value To Servo Movement

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


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

United Kingdom
#1  

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;

$value=GetADC(ADC0)

Then you convert that to the servo position;

$position = $value/100

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

$position = Round($position,0)

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

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).


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

United Kingdom
#2  

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

#3   — Edited

Thank You Rich. I added one thing to your code and it allowed me to make the servo run full range.  The code as was only gave me about 90 degrees of movement. Here is what I ended with:

/ ADC to servo movement

:loop $value=GetADC(ADC0) $position = $value/100 $position = Round($position,0) $position = Map($value, 54, 129, 180, 1 ) #adjusted first two values to read min/max read ADC project, 2nd two for direction (reverse 1 and 180). Servo(D0,$position) Sleep(250) Goto(loop)

Thanks again for the boost to get me there!