PDA

View Full Version : Code help; BS1/PROP1 for..next loops not delaying?



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

Jon Williams
01-29-2006, 05:58 AM
With the BS1/Prop-1 you have to use STEP when the start value is less than the end value, like this:

Start:
· FOR counter = 40 TO 1 STEP -1
··· DEBUG CLS, "Ready in ", #counter, "seconds."
··· PAUSE 800
· NEXT
· DEBUG CLS, "Waiting for trigger..."

When you added DEBUG you were getting a bit of a delay because of the DEBUG output.· Another note: DEBUG takes about 200 milliseconds, so you may want to change your PAUSE 1000 in that loop to PAUSE 800.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Post Edited (Jon Williams (Parallax)) : 1/28/2006 11:02:32 PM GMT

Bruce Bates
01-29-2006, 05:58 AM
CBinVa -

When you use a decrementing FOR ... NEXT loop on a BS-1 you need to specify a negative STEP parameter, as follows:

FOR counter = 40 TO 0 STEP -1

Regards,

Bruce Bates

CBinVa
01-29-2006, 06:14 AM
I'll mod the STEP on the FOR..NEXT's... though that first loop (40..0) was the only one that DOES work.

It's all the other loops (which are positive increments) further down that just drop right through. ??
0 to 300.. 0 to idletime... 0 to triggerdelay... all STEP 20s (since I do a 20Ms pause in each iteration).

·-Chris
·

Jon Williams
01-29-2006, 06:56 AM
Chris,

I think the problem his your end-values in the FOR-NEXT are off by a factor of 10 (based on your comments).· Since I'm working on some Prop-1 code today anyway (prepping for HauntX), I took the liberty of reformatting and updating your code -- it has opportunities for improvement that will use less code space and make updating easier.· See attached listing.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Post Edited (Jon Williams (Parallax)) : 1/29/2006 12:46:08 AM GMT

CBinVa
01-29-2006, 08:25 AM
Eureka... Or more appropriately, DUH!

You got it. Fixed the delay counters and now things are creeping along just right http://forums.parallax.com/images/smilies/yeah.gif

Thanks!

On an unrelated note; I hacked a couple radio-shack 20-second recording modules (per instructions at http://www.employees.org/~joestone/Halloween.old/Html/microcontrollers.htm

Have the switch on the recording module wired through GND and Out0 on my PROP1, and have the unit hooked up to its own 9V battery. The Prop1 is switched to power "2" setting to power an external servo.

Everything WORKS fine, but...

The V+ led is lighting up when the recording module is hooked up and battery-powered. Do I need a resistor in line with the switch on the recording module? Or is the V+ led lighting up normal?

Also, is there an easy way to get 9V power off the PROP1, since I'll be powering it via a 12V wall-wort anyway?

Thanks again!
·-Chris

·

Jon Williams
01-29-2006, 09:03 AM
I know Joe Stone -- he's a great guy and has given us valuable feedback on the Prop-1 and other products.

The LED is connected to the V+ pin so if there is any power there·the LED will light.· Just make sure you don't ever put the power switch into position 2 when you're using an external supply like that·or else your Prop-1 power input will conflict with the audio board power and you'll have a problem.·· I'm betting that you could feed 12 volts into that boad -- I've seen them and they have a small 5v regulator onboard.· It has a 9v battery clip purely for convenience.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax