Shop OBEX P1 Docs P2 Docs Learn Events
how to make this code stop when it reaches 20bps. — Parallax Forums

how to make this code stop when it reaches 20bps.

bobsmithbobsmith Posts: 36
edited 2004-09-18 16:08 in BASIC Stamp
How do I make this program stop when it reaches 20 balls per second

work var byte
work =0

Input 1··'Button
output 3 ·'soleniod
low p3··'Make sure our display pin is low to begin with
Loop:
· Button 1,1,20,2,work,1,press
· pause 20
· goto loop
press:
· high P3
· pause 10
· low P3
· goto loop

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-18 16:08
    Before I suggest a solution, let me point out a few things that will save you troubles/code space:

    1) The BASIC Stamp initializes are vars to 0 -- you don't need the line "work = 0" at the top of your code.

    2) HIGH and LOW make pins outputs, so you don't need to use "OUTPUT pin" if you're going to use HIGH or LOW on that pin.

    3) LOOP is a PBASIC 2.5 keyword -- don't use it as a label name.

    4) The BUTTON command is not as useful as it would seem, especially if you have a delay right after it.

    5) Using PIN and CON to name IO ports and values will make your code easier to read.


    As far as your program, what you want to do is make every path through the program about 50 milliseconds (20 times per second). Then count the number of fire events you have -- if it ever reaches 20, you can exit. Something like this (that may need some refinement):

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Fire······· PIN··· 1
    Solenoid··· PIN··· 3

    Yes········ CON··· 1
    No··········CON··· 0

    shots······ VAR··· Byte


    Main:
    · DO
    ··· IF (Fire = Yes) THEN
    ····· shots = shots + 1
    ····· HIGH Solenoid
    ····· PAUSE 10
    ····· LOW Solenoid
    ····· PAUSE 40
    ··· ELSE
    ····· shots = 0
    ····· PAUSE 50
    ··· ENDIF
    · LOOP WHILE (shots < 20)
    · END


    If the Fire button is pressed then the shots counter is incremented; if not pressed the shots counter is cleard. In either case there is about a 50 ms worth of PAUSE timing, so the loop will run about 20 times per second.· When the shots counter reaches 20 the loop will terminate.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 9/18/2004 4:12:38 PM GMT
Sign In or Register to comment.