0

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
  • 1
    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
  • @smith8m3 - Here is a really important bit of knowledge for free. Whenever you deal with hardware, rather than software, you must be able to examine the hardware. This means a DMM at the least, and a scope is a very close second. If you don't do this, you can never hope to resolve the question: is this a hardware issue or a software issue? And whenever you have a malfunctioning bit of software-controlled hardware, this is the FIRST question you need to ask. So, "I don't see a need to use it." suggests that you are in for a great deal of trouble. – WhatRoughBeast Mar 18 '16 at 14:50

1 Answers1

1

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

Vicente Cunha
  • 2,642
  • 8
  • 12