The program needs to control several things including two servos. Here are the actions for the Porp2 controller.
1) Listen for the start buton push.
2) When the button is pushed:
a. Start sound card
b. Move bug servo
c. Drop elevator
d. Power wall motor
e. Move elevator dial servo
f. Flash lights
3) Listen for magnetic sensor on rotating wall panel. Stop wall motor when it triggers.
4) Wait a minute, then advance wall panel to next magnet marker.
5) Reset servos and lights.
6) Listen for the start buton push.
Here is the Basic Stamp II code:
' {$STAMP BS2}
' {$PBASIC 2.5}
'-----------I/O Definitions--------------------------
dial PIN 0
bug PIN 1
lights PIN 3
magnets PIN 4
wall PIN 5
sound PIN 6
red PIN 7
msensor PIN 14
trigger PIN 15
'----------Variable Definitions-------------------
pos1 VAR Word
pos2 VAR Word
t VAR Word
m VAR Word
i VAR Byte
DIRS = %0011111111111111
OUTS = %0000000000011000
pos1 = 760
pos2 = 295
'-------------Program Code----------------
Main:
GOSUB Update_servos
IF(trigger=1) THEN Check_trigger
PAUSE 15
GOTO Main
'------------pulse servos-----------------
Update_servos:
PULSOUT dial,(pos1)
PULSOUT bug,(pos2)
RETURN
'-----------listen for start button push----------
Check_trigger:
m=0
FOR t=1 TO 10
IF (trigger = 1) THEN m = m + 1
PAUSE 15
GOSUB Update_servos
NEXT
IF (m = 10) THEN Run_elevator
RETURN
'----------start elevator---------------
Run_elevator:
HIGH sound
FOR t=300 TO 1200 STEP 50 'move bug
PAUSE 20
pos2 =t - 5
GOSUB Update_servos
NEXT
FOR t=1 TO 50 'pause a second
PAUSE 20
GOSUB Update_servos
NEXT
LOW lights
LOW sound
LOW magnets 'drop elevator
HIGH wall 'start wall moving
FOR t=1 TO 4
PAUSE 20
GOSUB Update_servos
NEXT
HIGH lights
FOR t=1 TO 3
PAUSE 20
GOSUB Update_servos
NEXT
LOW lights
FOR t=1 TO 8
PAUSE 20
GOSUB Update_servos
NEXT
HIGH lights
FOR i=1 TO 225 'more lights flasing
IF i = 90 THEN LOW lights
IF i = 140 THEN HIGH lights
IF i = 143 THEN LOW lights
IF i = 146 THEN HIGH lights
FOR t=1 TO 2
GOSUB Update_servos
PAUSE 19
NEXT
pos1 = pos1 - 1 'moving elevator dial
NEXT
FOR i=1 TO 220 'more lights flashing. Also start listening for the magnetic sensor.
IF i = 50 THEN HIGH red
IF i = 55 THEN
HIGH lights
LOW red
ENDIF
IF i = 60 THEN
HIGH red
LOW lights
ENDIF
IF i = 135 THEN LOW red
IF i = 140 THEN HIGH lights
m=0
FOR t=1 TO 3 'listen for magnetic sensor
IF (msensor = 1)THEN m = m + 1
PAUSE 9
GOSUB Update_servos
NEXT
IF (m = 3) THEN 'if you detect the magnet, stop the wall and turn the lights red.
LOW wall
LOW lights
HIGH red
ENDIF
pos1 = pos1 - 1
NEXT
FOR i=1 TO 100 'still listening as a precausion
m=0
FOR t=1 TO 3
IF (msensor = 1)THEN m = m + 1
PAUSE 9
GOSUB Update_servos
NEXT
IF (m = 3) THEN
LOW wall
LOW lights
HIGH red
ENDIF
NEXT
LOW wall
LOW lights
HIGH red
FOR t=1 TO 2500 'pause 50 seconds
PAUSE 20
GOSUB Update_servos
NEXT
FOR t=1 TO 50 'reset dial servo
PAUSE 20
pos1=pos1 + 1
GOSUB Update_servos
NEXT
FOR t=1 TO 50 'reset bug servo
PAUSE 20
pos2=pos2-1
GOSUB Update_servos
NEXT
pos1 = 760
pos2 = 295
HIGH wall 'turn on wall motor
FOR i=1 TO 15
PAUSE 20
GOSUB Update_servos
NEXT
FOR i=1 TO 50 'listen for next magnet
m=0
FOR t=1 TO 3
IF (msensor = 1)THEN m = m + 1
PAUSE 10
GOSUB Update_servos
NEXT
IF (m = 3) THEN LOW wall
NEXT
LOW wall
HIGH magnets
RETURN
I am sure better code could be written to do this but it works. The key thing if you make changes is that you keep pulsing the servos every 20 miliseconds or so. On to the next thing...
HomeCopyright © 2012 Garageofterror.com