0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Do you have an oscilloscope? \$\endgroup\$ – Spehro Pefhany Mar 18 '16 at 13:02
  • \$\begingroup\$ I'm not using one for this program. I didn't see a need to use it. Do you think it would help? \$\endgroup\$ – smith8m3 Mar 18 '16 at 13:08
  • 1
    \$\begingroup\$ Oh, yes, definitely. Otherwise you're working blind. \$\endgroup\$ – Spehro Pefhany Mar 18 '16 at 13:11
  • \$\begingroup\$ Are you giving the variable "time" an initial value? \$\endgroup\$ – Nedd Mar 18 '16 at 13:55
  • \$\begingroup\$ 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. \$\endgroup\$ – Nedd Mar 18 '16 at 14:04
1
\$\begingroup\$

"Servo freaks out and starts twitching in one spot."

Check your connections to the servo, this sounds like an issue I had in my starting grad years: unshared GND. If you are using an external source to power the servo, the external source GND should be connected to the GND of the microcontroller. Otherwise, your servo control signal will not be referenced appropriately and will lead to "undefined behavior" in the form of twitching.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.