Reverse direction for servo via potentiometer?

BotB

Member
Hi.

I have one Pot controlling the direction of two servos however I need one to turn in the opposite direction to the other when the pot is turned...

I believe it can be done by changing the maths that follows the "analogvalue" or wordcount in the lines below.

readadc analogport1, analogvalue1
analogvalue1 = analogvalue1 *10/17+75
servopos servoport1, analogvalue1


I believe it would have to be set up something like this?

readadc analogport1, analogvalue1
readadc analogport1, analogvalue2
analogvalue1 = analogvalue1 *10/17+75
analogvalue2 = analogvalue2 *(new code here reversing the direction)
servopos servoport1, analogvalue1
servopos servoport2, analogvalue2


I'm stuck as what to write for the little maths line that follows the "analogvalue2 *" section in order to equaly reverse the direction of the servo, for instance if servo1 moves 20 degrees clockwise then servo2 should move 20 degrees anti-clockwise.

Any help would be most apreciated and please let me know if there's anything you want to know.

Many thanks!
 

BCJKiwi

Senior Member
A couple of observations;

Code:
[COLOR=#008000]readadc analogport1, analogvalue1
[COLOR=black];readadc analogport1, analogvalue2 ; this line not required as analogue value 1 and 2 will be the same.
[/COLOR]analogvalue1 = analogvalue1 *10/17+75
[/COLOR][COLOR=#008000]analogvalue2 = analogvalue[B][I][COLOR=black]1[/COLOR][/I][/B] *(new code here reversing the direction)
servopos servoport1, analogvalue1
servopos servoport2, analogvalue2[/COLOR]
The new code to reverse the 'sense' depends on the pot being linear.
readadc gives a range of 0 to 254, so reversal requires a subtraction of the readadc value from 254.

i.e. if the readadc is mid range (127), then 254 - 127 will = 127 so both are mid range.
if readadc is near the bottom - say 50, then 254-50 = 204 - near the top for the other unit.

adjust the math to suit the ranges being worked with.
 

BotB

Member
Ah I see the mistake I made with the "analogvalue2" there, that was dense.

The pots I'm using are indeed linear, so what maths should I do to create the reverse for analogvalue2?
 

BCJKiwi

Senior Member
Oops - sorry for any confusion on 254/255 - the range is of course 0 thru 255!

The analogvalue2 math could be done in one line (just need to do it before the analogvalue1 math!);
Code:
analogvalue2 = 255-analogvalue1 * 10 / 17 + 75
analogvalue1 = analogvalue1 *10 / 17 + 75
 

Wrenow

Senior Member
And, of course, you could always get a servo reverserm a servo that goes the other way, or, I believe, swap the pot and motor leads in ond\e servo. But where is the fun in that? Good solutions above, thanks for reminding me on the scaling - I have a project in the noodling stage that this stirred some neurons.

Cheers,

Wreno
 

boriz

Senior Member
SERVO command: 75 is all the way left, 225 is all the way right, 150 is centre. So using B0 for servo1 and B1 for servo2, when B0=151, you want B1=149, and when B0=140, you want B1=160 etc.

Try this: B1=150-B0+150
 

BotB

Member
Thanks all for the help! the code

"analogvalue2=255-analogvalue2
analogvalue2=analogvalue2 *10/17+75"

works perfect for me, I had to change the 255 value as my servo was not set the same as the other (my fault, should have done that before putting it in such a tight spot)

Many thanks again for the help!
 
Top