Shop OBEX P1 Docs P2 Docs Learn Events
Convert program to use BRANCH — Parallax Forums

Convert program to use BRANCH

Tom PTom P Posts: 97
edited 2007-11-08 18:18 in BASIC Stamp
I need some help: I want to convert the program below to use the BRANCH command instead of IF/THEN. How do I make the BRANCH statement line act to filter the 100/200/300/400 as does the If/THEN

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


x VAR Word
cnt VAR Word
FC VAR Word
lamp1 CON 0
lamp2 CON 1
lamp3 CON 2
lamp4 CON 3


scan:
COUNT 5,1,cnt
Fc = cnt * 10
DEBUG DEC FC,CR
IF FC <=100 THEN alarm1
IF FC <=200 THEN alarm2
IF FC <=300 THEN alarm3
IF FC <=400 THEN alarm4
PAUSE 10
GOTO scan


alarm1:
HIGH lamp1
PAUSE 10
LOW lamp1
GOTO scan

alarm2:
HIGH lamp2
PAUSE 10
LOW lamp2
GOTO scan

alarm3:
HIGH lamp3
PAUSE 10
LOW lamp3
GOTO scan

alarm4:
HIGH lamp4
PAUSE 10
LOW lamp4

Post Edited By Moderator (Chris Savage (Parallax)) : 11/6/2007 4:02:31 PM GMT

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-11-06 16:14
    Tom -

    Personally, I'd use the SELECT ... CASE statement as follows:

    SELECT FC

    CASE <=100
    GOTO alarm1

    CASE <=200
    GOTO alarm2

    CASE <=300
    GOTO alarm3

    CASE <=400
    GOTO alarm4

    ENDSELECT

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-06 16:35
    BRANCH (FC MIN 1 -1)/100,[noparse][[/noparse] alarm1, alarm2, alarm3, alarm4]
    othercases:
    



    The FC MIN 1 - 1 / 100 evaluates to zero in integer math for FC<=100. The MIN 1 is there to prevent it from going negative. Later cases evaluate to 1 for 101--200, and so on.

    Another way, one that works even when the thresholds are not evenly spaced, is the following:
    LOOKDOWN FC, <=[noparse][[/noparse] 100,200,300,400],N   ' exit with index N equal to the first true condition of <=
    BRANCH N, [noparse][[/noparse] alarm1, alarm2, alarm3, alarm4]
    



    The CASE syntax that Bruce suggested may be your best bet, because you can put the alarm code right there under each CASE without the GOTO. It will execute the first true CASE that it comes across and skip the others.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tom PTom P Posts: 97
    edited 2007-11-06 18:25
    Thanks to both Tracy and Bruce for their help in making me see two different ways of solving the problem
  • D FaustD Faust Posts: 608
    edited 2007-11-07 19:53
    FYI, if you care, the case stamement will be converted to IF THENs anyway before it is executed. The Branch will run diferently. Quick question: What is the diference between BRANCH and ON ... GOTO... Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-07 19:58
    I believe that the BRANCH and the ON ... GOTO ... get translated into the same sort of byte codes.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-11-07 20:07
    I found the "Lookdown... Branch" example very helpful. The problem with lots of "IF" statements is that each one has to execute before the next will be checked. The "Lookdown... Branch" combination does TWO executions to get to the destination -- very efficient, and doesn't take a lot of code, either.

    The ON..GOTO and ON..GOSUB constructs are PBasic 2.5. I assume the ON...GOTO will result in the PBasic 'tokens' for the Branch statement. The On..GOSUB I'm less sure of.
  • D FaustD Faust Posts: 608
    edited 2007-11-07 20:14
    Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-07 22:00
    If you're interested, I have a writeup about how the codes that were new to PBASIC 2.5 translate back to the original PBASIC tokens, and tips for efficiency, posted at BS2pbasic25.htm
    The BRANCH and ON ... GOTO syntax do both produce the same tokens, and BRANCH is indeed more efficient coding than a string of IF THENs as is used in the SELECT-CASE statement. ON ... GOSUB is also efficiently coded by PBASIC 2.5, using BRANCHes but with a tricky microcoding of the RETURN instruction that Jeff Martin came up with that does not have an exact equivalent in the original PBASIC.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tom PTom P Posts: 97
    edited 2007-11-08 16:58
    Tracy or Bruce:

    I decided to try and use the CASE/SELECT command.
    I would like to experiment a bit.
    I wanted to test a range between for example:
    100 to 300 CASE 1
    300 to 500 CASE2
    500 to 700 CASE 3 .......
    How would I test for RANGES of numbers in the CASE/SELECT example?

    Thanks
    TOm
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-11-08 17:27
    Tom -

    You need to take a look at the CASE ... SELECT statement in the PBASIC Reference Manual, or the PBASIC Help FIle, as in both places they give examples of exactly what you're looking to do. Here is a starter, but you will have to determine where you actually want the end limits to go (300 and 500), as I suspect you want them to go one place or the other:

    SELECT range

    CASE 100 to 299
    'Code you wish to execute

    CASE 300 to 499
    'Code you wish to execute

    CASE 500 to 700
    'Code you wish to execute

    ENDSELECT

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 11/8/2007 5:41:07 PM GMT
  • Tom PTom P Posts: 97
    edited 2007-11-08 18:18
    That seems easy enough - didn't realize it was that easy to do!
    thanks
    Tom
Sign In or Register to comment.