# How to control the speed of rotation of a servo using a potentiometer?

I'm trying to write a program using BASIC to turn a servo 180 degrees back and forth, and to use a potentiometer to control the speed at which the servo turns. I have a code, but when I run it, the servo freaks out and starts twitching in one spot, and I don't know why it's doing that or what's causing it. Can someone explain to me why it's doing that, what's causing it, or a way to fix it?

' What's a Microcontroller - ControlServoWithPotUsingDirectives.bs2
' Read potentiometer in RC-time circuit using RCTIME command.
' Apply scale factor and offset, then send value to servo.
' {$STAMP BS2} ' {$PBASIC 2.5}

counter VAR Word

rcPin PIN 5                                       ' I/O Pin Definitions
servoPin PIN 14

scaleFactor CON 15                               ' Constant Declarations
offset CON 1
delay CON 10

time VAR Word                                     ' Variable Declaration

PAUSE 1000                                        ' Initialization

DO
' Main Routine
HIGH rcPin                                       ' RC decay measurement
PAUSE 100
RCTIME rcPin, 1, time
'time = time * scaleFactor                       ' Scale scaleFactor.
'time = time + offset                             ' Offset by offset.
'PULSOUT servoPin, time                           ' Send pulse to servo.
'DEBUG HOME, DEC5 time                            ' Display adjusted time value.

FOR counter = 300 TO 1100 STEP time
PULSOUT servoPin, counter
PAUSE 7
DEBUG DEC5 counter, CR
DEBUG DEC5 time, CR
NEXT

PAUSE 10

FOR counter = 1100 TO 300 STEP time
PULSOUT servoPin, counter
PAUSE 7
DEBUG DEC5 counter, CR
DEBUG DEC5 time, CR
NEXT

LOOP

• Do you have an oscilloscope? – Spehro Pefhany Mar 18 '16 at 13:02
• I'm not using one for this program. I didn't see a need to use it. Do you think it would help? – smith8m3 Mar 18 '16 at 13:08
• Oh, yes, definitely. Otherwise you're working blind. – Spehro Pefhany Mar 18 '16 at 13:11
• Are you giving the variable "time" an initial value? – Nedd Mar 18 '16 at 13:55
• If it is not a programming issue, it may be that the potentiometer has a bad spot in its travel. A voltmeter may help to watch for erratic voltage swings on the wiper pin while you adjust it. As Spehro suggest using a scope at that point would be best. – Nedd Mar 18 '16 at 14:04