| adjusted the subroutine code so that after the gun turns to position and the dc motor has come up to speed it will pings again to see if the target is still there.
Also after each trigger pull it pings to see if the target is still there. If yes it keeps firing (pinging between each fire) until the target leaves or it runs out of ammo.
Heres the revised code
' {$STAMP BS2} ' {$PBASIC 2.5}
'----------------------------pin locations for each component---------------------------------------------------------- 'p6 = ping sensor 'p8 = relay to activate dc motor 'p10 = piezo speaker 'p12 (servo plug) = sweeping servo that ping sensor is attached to 'p13 (servo plug) = servo that gun is attached to 'p14 (servo plug) = servo that activates mechanical trigger on toy gun
'----------------------------constants and variables--------------------------------------------------------------------
inconstant CON 890 'constant to divide raw ping data to convert to inches. Room Temp counterstep CON 150 'this is the amount of steps the sweep will go through.
'-----------------change this constant to change the trigger range- This number is in inches---------------------------- trigger CON 15 'trigger distance '----------------------------------------------------------------------------------------------------------------------- direction VAR Word ammo VAR Word counter VAR Word 'this counter is for the servo sweep sweep VAR Word gunsweep VAR Word 'this counter repeats the sweep until servo is in position indistance VAR Word time VAR Word beeper VAR Word
'---------------------sets trigger servo to neutral start position and motor relay to off---------------------------- LOW 8 'sets fire relay to off
FOR counter = 1 TO 20 'sets trigger servo to neutral PULSOUT 14,750 PAUSE 20 NEXT
'---------------------beeps 5 times then slowly scans ping servo right to left (just for show!)---------------------- FOR counter = 1 TO 5 'beep at start FREQOUT 10, 250, 3500 PAUSE 100 NEXT 'sweep to start position - just for show FOR counter = 1000 TO 500 STEP 3
PULSOUT 12, counter PULSOUT 13, counter PAUSE 10 NEXT
FOR counter = 1 TO 20 'sets gun to neutral PULSOUT 13,750 PAUSE 20 NEXT
'---------------------------------------starts scanning/firing sequence----------------------------------------------- DO 'start scanning FOR direction = 500 TO 1050 STEP counterstep FOR sweep = 1 TO 6 PULSOUT 12, direction
PAUSE 10 NEXT
PULSOUT 6,5 'ping PULSIN 6,1,time
indistance = inconstant ** time ' convert ping input to inches
DEBUG HOME, DEC5 time, " ping raw data", CR, 'displays raw ping data DEC3 indistance, " in", CR, 'displays ping results in inches DEC2 ammo, " ammo count" 'displays the amount of ammo left PAUSE 10
IF indistance = <trigger THEN GOSUB fire ELSE ENDIF
NEXT
FOR direction = 900 TO 550 STEP counterstep FOR sweep = 1 TO 6 PULSOUT 12, direction
PAUSE 10 NEXT
PULSOUT 6,5 'ping PULSIN 6,1,time
indistance = inconstant ** time ' convert ping input to inches
DEBUG HOME, DEC5 time, " ping raw data", CR, 'displays raw ping data DEC3 indistance, " in" 'displays ping results in inches
PAUSE 10
IF indistance = <trigger THEN 'triggers firing sequence GOSUB fire ELSE ENDIF
NEXT
LOOP
fire: 'activates firing relay
HIGH 8
FOR gunsweep = 1 TO 10 'move to fire position of ping servo PULSOUT 13, direction PAUSE 20 NEXT
PAUSE 250 'pause long enough for dc motor to spin up to speed
PULSOUT 6,5 'ping to check if target is still in location PULSIN 6,1,time
indistance = inconstant ** time ' convert ping input to inches
IF indistance = <trigger THEN GOSUB triggerpull ELSE LOW 8
FOR gunsweep = 1 TO 10 'return to center after fire PULSOUT 13, 750 PAUSE 20 NEXT
ENDIF RETURN
triggerpull: ammo = ammo + 1 IF ammo = 11 THEN 'check how much ammo is left GOSUB ammoout ELSE ENDIF FOR counter = 1 TO 20 'servo to activate mechanical trigger PULSOUT 14,600 PAUSE 20 NEXT FOR counter = 1 TO 20 'return trigger servo to neutral PULSOUT 14,750 PAUSE 20 NEXT PULSOUT 6,5 'ping to check if target is still in location PULSIN 6,1,time
indistance = inconstant ** time 'convert ping input to inches
IF indistance = <trigger THEN GOSUB triggerpull ELSE 'turn off relay LOW 8 FOR gunsweep = 1 TO 10 'return to center after fire PULSOUT 13, 750 PAUSE 20 NEXT ENDIF
RETURN
'------------------------if ammo out return servos to neutral and beep 10 times then shutdown--------------------------- ammoout:
FOR gunsweep = 1 TO 10 'return to center after ammo out PULSOUT 13, 750 PULSOUT 12, 750 PAUSE 20 NEXT
FOR beeper = 1 TO 10 FREQOUT 10, 250,3500 PAUSE 100 NEXT
END |