IF-AND-question
Moskog
Posts: 554
Hey everybody!
I wonder if there is a better way in Pbasic to decide action then the example below:
IF·pulses >=0 AND·pulses <11 THEN GOSUB action1
IF·pulses >10 AND·pulses <72 THEN GOSUB action2
IF·pulses >71 AND·pulses <158 THEN GOSUB action3
..and so on!
Pulses can be any numbers in between. Lets say pulses is 30 then action2 will be done.
In my weather program, a subroutine·contains 13 lines of IF-statements like this to decide
what text string to be selected on·a LCD display. Like Calm, Breeze, Storm and so on.
Can LOOKDOWN be used in this case? I'm using BS2.
Kjell Romma, Norway
Post Edited (Moskog) : 11/30/2007 10:00:44 AM GMT
I wonder if there is a better way in Pbasic to decide action then the example below:
IF·pulses >=0 AND·pulses <11 THEN GOSUB action1
IF·pulses >10 AND·pulses <72 THEN GOSUB action2
IF·pulses >71 AND·pulses <158 THEN GOSUB action3
..and so on!
Pulses can be any numbers in between. Lets say pulses is 30 then action2 will be done.
In my weather program, a subroutine·contains 13 lines of IF-statements like this to decide
what text string to be selected on·a LCD display. Like Calm, Breeze, Storm and so on.
Can LOOKDOWN be used in this case? I'm using BS2.
Kjell Romma, Norway
Post Edited (Moskog) : 11/30/2007 10:00:44 AM GMT
Comments
CheckIR:·'Record Routine
···· GOSUB drive·'resets encoder count to 0
· DO
···· GOSUB encoder
···· 'DEBUG DEC ? encodercount,CR
···· Process_IR_Pulses:····························· ·'Subroutine
····· check_for_stop_bit:
····PULSIN IRdetector,active_high,IR_pulse(0)·'save pulse value
······· IF IR_pulse(0) > 1400 AND IR_pulse(0) <> 0 THEN continue 'stopbit filter
····· GOTO check_for_stop_bit························ 'If no stop-bit detected
·····'> 1400 count (*2=2800 us = 2.8 ms)
·····'and not equal to 0
····· continue:
····· '······ pin, 0, array position
····· PULSIN IRdetector,active_low,IR_pulse(0)
····· PULSIN IRdetector,active_low,IR_pulse(1)
····· PULSIN IRdetector,active_low,IR_pulse(2)
····· PULSIN IRdetector,active_low,IR_pulse(3)
····· PULSIN IRdetector,active_low,IR_pulse(4)
····· PULSIN IRdetector,active_low,IR_pulse(5)
····· PULSIN IRdetector,active_low,IR_pulse(6)
'······ looks for a “0” and stores the index position
····· FOR counter = 0 TO 6
······ LOOKDOWN IR_pulse(counter), < [noparse][[/noparse]400,800], IR_message.LOWBIT(counter)
····· NEXT
· ·IF IR_message = power THEN user_pressed_power·'power = 21 count = 42 us
· ·LOOKUP IR_message,[noparse][[/noparse]1,2,3,4,5,6,7,8,9,0],IR_message'keypad number corresponding to ir message
· ·entered_value = entered_value * 10 + IR_message
· ·PAUSE debounce_time·'pause is con 200, = 400 us
· ·'DEBUG "You pressed ", DEC1 IR_message, CR
·· ··SELECT ir_message·'doesn't have to be sequential
· ·· ·CASE=1:speed="2":GOSUB drive·'affects turn (time based)
· ·· ·CASE=2:speed="5":GOSUB drive
· ·· ·CASE=3:speed="9":GOSUB drive
· ·· ·CASE=4:turn=255 : motion=1:GOSUB record:GOSUB steer:GOSUB drive:GOSUB encoder
· ·· ·CASE=5:turn=128 : motion=1:GOSUB record:GOSUB steer:GOSUB drive:GOSUB encoder
· ·· ·CASE=6:turn=1·· : motion=1:GOSUB record:GOSUB steer:GOSUB drive:GOSUB encoder
· ·· ·CASE=7:GOTO user_pressed_power
· ·· ·CASE=8:turn=128:motion=0:GOSUB record:GOSUB steer:GOSUB drive:GOSUB encoder·'could also use On Gosub or Branch (sequential?).
· ·· ·CASE=0:speed="0":GOSUB drive
···ENDSELECT
· LOOP
· RETURN
If Pulses·>158·THEN·GOTO ...
If Pulses·>71·THEN·GOTO ...
If Pulses·>10·THEN·GOTO ...
If Pulses > 0 THEN GOTO ...
If Pulses = 0 Then GOTO ...
When the program runs from the top down, it jumps to the first "goto" that satisfies the criteria and avoids the rest.· It's a little trickier than using·"GoSub", but whatever statement it jumps to can use "Goto" to jump back (to the next part of the programme after the "If Then" statements).·
Although I agree with the use of SELECT...CASE, converting your sample coding from IF...THEN format to SELECT...CASE format may make it a bit more clear:
{PBASIC 2.5}
SELECT pulses
CASE >=0 and <=10
· GOTO action1
CASE >=11 and <=71
· GOTO action2
CASE >=72 and <=157
· GOTO action3
ENDSELECT
(NOTE: Use GOTO rather than GOSUB as there are no overlapping values)
Regards,
Bruce Bates
or the actions could be SELECT enCASEd.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
and even I'm deep into the reference book every day I do appreciate ideas from all of you
out there!
Kjell Romma, Norway