Parallax servos rotate randomly
jmerrell
Posts: 25
I am trying to rotate 2 servos independently, which are each activated from a laser beam which hits a photo diode for each servo. Other minor circuitry in schematic. I would like each servo to rotate as its photo diode is hit with the laser and NOT rotate again until it is hit with the laser again, however the servos rotate at random...once at the start of the program and then after being hit with the laser. Is there someone who knows why? My PBASIC program is shown below. Thanks!
' {$STAMP BS2}
' {$PBASIC 2.5}
'========DIR===========================================
'OUTH = %00000000
'DIRH = %11111111
'=======VARIABLES======================================
counter VAR Word
'=======MAIN===========================================
MAIN:
DO
IF IN1 = 1 THEN
GOSUB TARGET1
ELSEIF IN2 = 1 THEN
GOSUB TARGET2
ELSEIF IN3 = 1 THEN
GOSUB TARGET3
ELSEIF IN4 = 1 THEN
GOSUB TARGET4
ENDIF
LOOP
'========SUBROUTINES====================================
Target1:
DEBUG "Target 1 hit", CR 'Target 1 hit"
FOR counter = 1 TO 25
PULSOUT 15,1400 'Servo rotates DOWN"
PAUSE 20
NEXT
FOR counter = 1 TO 25
PULSOUT 15, 660 'Servo rotates UP"
PAUSE 20
NEXT
PAUSE 2000
LOW IN1
RETURN
'
Target2:
DEBUG "Target 2 hit", CR 'Target 2 hit"
FOR counter = 1 TO 25
PULSOUT 14,1400 'Servo rotates DOWN"
PAUSE 20
NEXT
FOR counter = 1 TO 25
PULSOUT 14, 660 'Servo rotates UP"
PAUSE 20
NEXT
PAUSE 2000
'LOW IN2
RETURN
' {$STAMP BS2}
' {$PBASIC 2.5}
'========DIR===========================================
'OUTH = %00000000
'DIRH = %11111111
'=======VARIABLES======================================
counter VAR Word
'=======MAIN===========================================
MAIN:
DO
IF IN1 = 1 THEN
GOSUB TARGET1
ELSEIF IN2 = 1 THEN
GOSUB TARGET2
ELSEIF IN3 = 1 THEN
GOSUB TARGET3
ELSEIF IN4 = 1 THEN
GOSUB TARGET4
ENDIF
LOOP
'========SUBROUTINES====================================
Target1:
DEBUG "Target 1 hit", CR 'Target 1 hit"
FOR counter = 1 TO 25
PULSOUT 15,1400 'Servo rotates DOWN"
PAUSE 20
NEXT
FOR counter = 1 TO 25
PULSOUT 15, 660 'Servo rotates UP"
PAUSE 20
NEXT
PAUSE 2000
LOW IN1
RETURN
'
Target2:
DEBUG "Target 2 hit", CR 'Target 2 hit"
FOR counter = 1 TO 25
PULSOUT 14,1400 'Servo rotates DOWN"
PAUSE 20
NEXT
FOR counter = 1 TO 25
PULSOUT 14, 660 'Servo rotates UP"
PAUSE 20
NEXT
PAUSE 2000
'LOW IN2
RETURN
Comments
Once at the start of the program, then after being hit with the laser? That does not sound random. It is normal for servos to twitch briefly when powered up - and aren't they supposed to rotate after being lasered?
I would add some code before the main loop to center the servos, that may help.
Also, it is important that the power supply for the servos be adequate. A 9V battery is not.
They likely keep rotating since they aren't told to stop rotating.
+1 To Rich's 9V battery advice. Please don't use a 9V as servo power.
What is the BOE power supply? Is it a 9V battery, four AA batteries or a wall wart? Do not use a 9V!