CBinVa
01-29-2006, 05:42 AM
Maybe I'm overwriting something, or my 'counter' variable isn't getting accessed like I think...
But in the code below, my first delay loop (waiting for the IR detector to initialize) works ok; but all the loops after that just drop through (maybe one pass, can't tell).
When I added DEBUG lines in the loops showing the counter at each iteration, the delays WORKED?! Remove the DEBUG lines and it drops through.
Did I miss something simple? A typo I missed? Or am I not accounting for something.
Thanks; below is the code.
·-Chris
' {$STAMP BS1}
' {$PBASIC 1.0}
' -----[ I/O Definitions ]-------------------------------------------
SYMBOL PIR=7
SYMBOL EYES=1
SYMBOL SERVO=2
SYMBOL SoundEffect=0··· ' sound card on out0
SYMBOL fan=5
SYMBOL COUNTER=W2
SYMBOL HeadLeft=55····· ' code for servo full left
SYMBOL HeadRight=230··· ' code for servo full right
SYMBOL TriggerDelay=300 ' delay between motion detected and start
SYMBOL IdleTime=500···· ' delay between reactivations
' -----[ Initialization ]--------------------------------------------
DEBUG CLS················· ' Clear DEBUG Screen
FOR counter = 40 TO 0····· ' Wait 40 Seconds For PIR Warm-Up
DEBUG "IR Ready in:", counter 'print how much time left
· PAUSE 1000·············· ' Display Counter Every Second
DEBUG CLS················· ' Clear DEBUG Screen
NEXT
counter = 0··············· ' Clear Counter Variable
DEBUG "WAITING...·· "····· ' Display Waiting Message
' -----[ Program Code ]-----------------------------------------
WaitForMotion:
··· PULSOUT servo,HeadLeft
··· LOW EYES
··· PAUSE 20
··· IF PIN7 <> 1 THEN WaitForMotion············· ' Motion Detected?
··· DEBUG "TRIPPED..."
PropSequence:
' motion detected, wait 'triggerdelay' seconds before turning head
··· FOR counter=0 TO TriggerDelay STEP 20
····· GOSUB TurnHeadLeft···· ' hold head in left position
····· PAUSE 20
··· NEXT
DEBUG "Head Right"
' turn head right and wait 3 seconds
··· counter=0
··· FOR counter=0 TO 300 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Light eyes"
··· HIGH EYES· ' light up the eyes
' 2 second delay before screaming...
··· counter=0
··· FOR counter=0 TO 200 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Scream..."
··· GOSUB Scream
··· DEBUG "15 second delay..."
' 15 second delay while screaming
··· counter=0
··· FOR counter=0 TO 1500 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Head (back) Left"
··· GOSUB TurnHeadLeft ' head back left
··· counter=0
··· FOR counter=0 TO IdleTime STEP 20
····· GOSUB TurnHeadLeft
····· PAUSE 20
··· NEXT
··· DEBUG "Resetting"
GOTO WaitForMotion
TurnHeadLeft:
· PULSOUT servo,HeadLeft
· RETURN
TurnHeadRight:
· PULSOUT servo,HeadRight
· RETURN
Scream:
· LOW SoundEffect
· HIGH SoundEffect
· RETURN
But in the code below, my first delay loop (waiting for the IR detector to initialize) works ok; but all the loops after that just drop through (maybe one pass, can't tell).
When I added DEBUG lines in the loops showing the counter at each iteration, the delays WORKED?! Remove the DEBUG lines and it drops through.
Did I miss something simple? A typo I missed? Or am I not accounting for something.
Thanks; below is the code.
·-Chris
' {$STAMP BS1}
' {$PBASIC 1.0}
' -----[ I/O Definitions ]-------------------------------------------
SYMBOL PIR=7
SYMBOL EYES=1
SYMBOL SERVO=2
SYMBOL SoundEffect=0··· ' sound card on out0
SYMBOL fan=5
SYMBOL COUNTER=W2
SYMBOL HeadLeft=55····· ' code for servo full left
SYMBOL HeadRight=230··· ' code for servo full right
SYMBOL TriggerDelay=300 ' delay between motion detected and start
SYMBOL IdleTime=500···· ' delay between reactivations
' -----[ Initialization ]--------------------------------------------
DEBUG CLS················· ' Clear DEBUG Screen
FOR counter = 40 TO 0····· ' Wait 40 Seconds For PIR Warm-Up
DEBUG "IR Ready in:", counter 'print how much time left
· PAUSE 1000·············· ' Display Counter Every Second
DEBUG CLS················· ' Clear DEBUG Screen
NEXT
counter = 0··············· ' Clear Counter Variable
DEBUG "WAITING...·· "····· ' Display Waiting Message
' -----[ Program Code ]-----------------------------------------
WaitForMotion:
··· PULSOUT servo,HeadLeft
··· LOW EYES
··· PAUSE 20
··· IF PIN7 <> 1 THEN WaitForMotion············· ' Motion Detected?
··· DEBUG "TRIPPED..."
PropSequence:
' motion detected, wait 'triggerdelay' seconds before turning head
··· FOR counter=0 TO TriggerDelay STEP 20
····· GOSUB TurnHeadLeft···· ' hold head in left position
····· PAUSE 20
··· NEXT
DEBUG "Head Right"
' turn head right and wait 3 seconds
··· counter=0
··· FOR counter=0 TO 300 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Light eyes"
··· HIGH EYES· ' light up the eyes
' 2 second delay before screaming...
··· counter=0
··· FOR counter=0 TO 200 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Scream..."
··· GOSUB Scream
··· DEBUG "15 second delay..."
' 15 second delay while screaming
··· counter=0
··· FOR counter=0 TO 1500 STEP 20
····· GOSUB TurnHeadRight
····· PAUSE 20
··· NEXT
··· DEBUG "Head (back) Left"
··· GOSUB TurnHeadLeft ' head back left
··· counter=0
··· FOR counter=0 TO IdleTime STEP 20
····· GOSUB TurnHeadLeft
····· PAUSE 20
··· NEXT
··· DEBUG "Resetting"
GOTO WaitForMotion
TurnHeadLeft:
· PULSOUT servo,HeadLeft
· RETURN
TurnHeadRight:
· PULSOUT servo,HeadRight
· RETURN
Scream:
· LOW SoundEffect
· HIGH SoundEffect
· RETURN