' ========================================================================= ' ' File....... Crate_v2.BS1 ' Purpose.... ' Original Author..... Jon Williams -- Parallax, Inc. ' E-mail..... jwilliams@parallax.com ' Modified by: PerfessorEvil www.perfessorevil.com ' Started.... ' Updated.... 15 OCT 2005 ' ' {$STAMP BS1} ' {$PBASIC 1.0} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' Thashing crate controller -- thrashes when triggered. ' ' P6 is used as the active-high trigger. Move P6 SETUP jumper to DN and ' connect a normally open switch (button, mat switch) between P6.W and ' P6.R. ' ' If using a PIR, connections are: ' ' PIR out --> P6.W ' PIR Vcc --> P6.R ' PIR Gnd --> P6.B ' ' Make sure your PIR is a 5-volt unit and has an active-high output. ' -----[ Revision History ]------------------------------------------------ ' -----[ I/O Definitions ]------------------------------------------------- SYMBOL Trigger = PIN6 SYMBOL Crate = 5 SYMBOL FOG = 3 SYMBOL LAUGH = 2 ' -----[ Constants ]------------------------------------------------------- SYMBOL Yes = 1 SYMBOL No = 0 ' -----[ Variables ]------------------------------------------------------- SYMBOL lottery = W0 ' random number SYMBOL tix = W1 ' timer SYMBOL shakeTime = W2 ' time for shaking SYMBOL shakeAcc = W3 ' shake accumulator ' -----[ EEPROM Data ]----------------------------------------------------- ' -----[ Initialization ]-------------------------------------------------- Reset: LOW Crate ' make output and off lottery = 1031 ' seed random generator LOW LAUGH HIGH FOG PAUSE 300 ' let PIR warm up GOSUB Run_Timer ' -----[ Program Code ]---------------------------------------------------- Main: RANDOM lottery ' toss random generator IF Trigger = No THEN Main ' DEBUG "Triggered", CR LOW FOG HIGH LAUGH Pre_Shake_Delay: tix = lottery // 31 + 20 ' delay 2 to 5 seconds GOSUB Run_Timer shakeTime = lottery // 200 + 10 ' shake 1 to 20 secs shakeAcc = 0 ' clear accumulator Shake_Crate: RANDOM lottery ' toss random generator TOGGLE Crate ' toggle cylinder control ' DEBUG "crate Toggle", CR tix = lottery // 5 + 1 ' piston move, 0.1 to 1.5 secs shakeAcc = shakeAcc + tix ' update accumulator GOSUB Run_Timer IF shakeAcc < shakeTime THEN Shake_Crate ' done shaking? LOW Crate HIGH FOG LOW LAUGH ' back to resting position ' DEBUG "Crate off" , CR Trigger_Delay: tix = 100 ' delay at least 10 secs GOSUB Run_Timer GOTO Main ' -----[ Subroutines ]----------------------------------------------------- ' Pass timing units (0.1 sec) in "tix" ' -- "tix" is destroyed by routine Run_Timer: IF tix = 0 THEN Timer_Exit ' expired? PAUSE 100 ' no, pause tix = tix - 1 ' and decrement GOTO Run_Timer ' test again Timer_Exit: RETURN