' © Wolfstone, 2005 ' ========================================================================= ' ' File....... BeastOperation.bs1 ' Purpose.... Animate the Crate Beast ' Author..... Dennis Griesser ' E-mail..... wolfstone@pobox.com ' ' {$STAMP BS1} ' {$PBASIC 1.0} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' ' Control program for the Crate Beast Halloween prop. ' ' For information about the I/O, peripheral boards, etc, please see ' BeastControllerSetup.txt ' ' -----[ Revision History ]------------------------------------------------ ' ' BeastOperation_01.bs1 - 24 October 2005 ' BeastOperation_02.bs1 - 29 October 2005 ' BeastOperation_03.bs1 - ' BeastOperation_04.bs1 - 14 October 2007 - First version practical to use. ' Supports triggered beast and lid. ' BeastOperation_05.bs1 - 16 October 2007 - Add sound and autonomous mode. ' BeastOperation_06.bs1 - 16 October 2007 - Version for code review. ' -----[ I/O Definitions ]------------------------------------------------- SYMBOL TrigBeast = PIN7 ' input trigger on P7 SYMBOL TrigLid = PIN6 ' input trigger on P6 SYMBOL RedLED = PIN5 ' output debug feedback on P5 SYMBOL GreenLED = PIN4 ' output debug feedback on P4 SYMBOL Sio = 0 ' serial I/O on P0 ' -----[ Constants ]------------------------------------------------------- ' Communication with DC-16 Digital Control Board: SYMBOL Baud = OT2400 ' serial rate to talk to DC-16 SYMBOL Addr = %00 ' address of DC-16 board (%00 - %11) ' Solenoids: SYMBOL DoorOpen = 1 ' Open Door SYMBOL DoorClose = 2 ' Close Door SYMBOL ArmOpen = 3 ' Open Arm SYMBOL ArmClose = 4 ' Close Arm SYMBOL BeastOpen = 13 ' Open Beast SYMBOL BeastClose = 14 ' Close Beast SYMBOL LidOpen = 15 ' Open Lid SYMBOL LidClose = 16 ' Close Lid SYMBOL IsOff = 0 SYMBOL IsOn = 1 ' -----[ Variables ]------------------------------------------------------- SYMBOL RndNum = W0 ' 16-bit random number (and seed for next one) SYMBOL Solenoid = W1 SYMBOL Delay = W2 SYMBOL Cycles = B6 ' Using GOSUB on the BS1 requires variables B12 and B13 (W6) ' -----[ EEPROM Data ]----------------------------------------------------- ' -----[ Initialization ]-------------------------------------------------- Reset: ' 76543210 ' bit positions DIRS = %00111111 ' make P7-P6 inputs, P5-P0 outputs PINS = %00000000 ' all outputs off SEROUT Sio, Baud, ("!DC16", Addr, "X") ' turn off all solenoids PAUSE 1000 ' wait 1.0 second ' -----[ Program Code ]---------------------------------------------------- ' Reset all solenoids to Normally Closed position. ' We have to do this because the solenoids are bistable. Clear: SEROUT Sio, Baud, ("!DC16", Addr, "X") ' turn off all solenoids Solenoid = BeastClose ' turn on NC lid GOSUB Ping Solenoid = LidClose ' turn on NC lid GOSUB Ping Solenoid = ArmClose ' turn on NC lid GOSUB Ping Solenoid = DoorClose ' turn on NC lid GOSUB Ping ' The main loop just sits and spins until it sees an incoming command. Main: 'DEBUG "Main", CR IF TrigBeast = IsOn THEN DoBeast ' wait until trigger is activated IF TrigLid = IsOn THEN DoLid ' wait until trigger is activated ' Nothing going on! GOSUB TumbleRandom GOTO Main DonePerforming: ' Wait until input trigger is gone, in case the performance was very short. IF TrigBeast = IsOn THEN DonePerforming ' wait until trigger is gone IF TrigLid = IsOn THEN DonePerforming ' wait until trigger is gone GOTO Main ' This routine controls the whole "Show The Beast" function. ' After a nice Beast session, we return to the main loop. DoBeast: 'DEBUG "DoBeast", CR ' Open doors Solenoid = DoorOpen GOSUB Ping PAUSE 100 ' wait .1 second for doors to open ' Show Beast Solenoid = BeastOpen GOSUB Ping PAUSE 3000 ' wait 3.0 second for Beast to deploy and scare! ' Hide Beast Solenoid = BeastClose GOSUB Ping PAUSE 500 ' wait .5 second for Beast to withdraw ' Close doors Solenoid = DoorClose GOSUB Ping GOTO DonePerforming ' return to the main loop to await new command ' This routine controls the whole "Bang The Lid" function. ' After a good solid bang session, we return to the main loop. DoLid: 'DEBUG "DoLid", CR GOSUB TumbleRandom Cycles = RndNum & 7 IF Cycles > 2 THEN CyclesLidComputed Cycles = 2 ' minimum CyclesLidComputed: 'DEBUG Cycles FOR Cycles = Cycles TO 1 STEP -1 ' Open & close lid GOSUB BangLid ' Pause between bangs for small random time GOSUB TumbleRandom Delay = RndNum & 15 ' IF Delay > 3 THEN BangOpenPause ' Delay = 3 'BangOpenPause: Delay = Delay * 10 'DEBUG Delay PAUSE Delay NEXT GOTO DonePerforming ' return to the main loop to await new command END ' -----[ Subroutines ]----------------------------------------------------- ' Using GOSUB on the BS1 requires variables B12 and B13 (W6), making those ' variables unavailable for general program use. ' Fire one solenoid _coil_ and return it to rest state. ' Since the valves are bistable, it is a good idea to remove ' power after every operation is complete. Removing power does not ' change the position of the pneumatic valve! It just saves some ' power and precludes accidently energizing both the NC and NO ' solenoids. Ping: ' Fire the solenoid that we want, and wait for it to happen. ' According to our experiments (backed by the LTV data sheet), ' it takes up to 30 mSec for the valve to switch. SEROUT Sio, Baud, ("!DC16", Addr, "P", Solenoid, IsOn) ' turn on PAUSE 100 ' wait .10 second for valve to respond ' PAUSE 30 ' wait .03 second for valve to respond ' The valve has switched. Remove the solenoid power. SEROUT Sio, Baud, ("!DC16", Addr, "P", Solenoid, IsOff) ' turn off ' After sending a command, we must wait a little while for the parallel ' board to finish executing before we send another command. PAUSE 5 ' wait .005 second for parallel board RETURN ' Bang the lid up and down, once. BangLid: ' Open lid Solenoid = LidOpen GOSUB Ping ' PAUSE 100 ' Close lid Solenoid = LidClose GOSUB Ping ' PAUSE 300 RETURN ' Tumble random generator ' ' Each call to RANDOM generates 1 bit of entropy, with the previous ' 15 bits shifted to the left. This subroutine calls RANDOM several ' times in order to get more entropy. TumbleRandom: RANDOM RndNum RANDOM RndNum RANDOM RndNum RANDOM RndNum RANDOM RndNum RETURN