Convert program to use BRANCH
Tom P
Posts: 97
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
' {$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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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:
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--DFaust
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--DFaust
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
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
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
thanks
Tom