Shop OBEX P1 Docs P2 Docs Learn Events
IF-AND-question — Parallax Forums

IF-AND-question

MoskogMoskog Posts: 554
edited 2007-12-01 08:41 in BASIC Stamp
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

Comments

  • SandgroperSandgroper Posts: 62
    edited 2007-11-30 11:34
    I'm no expert, but you might try using an array variable with the LookDown, LookUp and SelectCase statements.··Have a look at "IR Remote for the BOE Bot" for some good examples on how to combine them (I've attached a snippet·that·I "recycled" for controlling a robot with the Sony IR remote).


    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
  • SandgroperSandgroper Posts: 62
    edited 2007-11-30 12:20
    You might also be able to·eliminate the "And" statement by sorting the statements from top to bottom, something like this:

    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).·
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-11-30 12:26
    Kjell -

    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
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-30 17:30
    I agree with Sandgroper, to use LOOKDOWN and ON .. GOSUB

    LOOKDOWN pulses,<=[noparse][[/noparse]10, 71, 157, 65535], fox
    ON fox GOSUB action1, action2, action3, action4
    



    or the actions could be SELECT enCASEd.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2007-12-01 08:41
    Thank you to all of you for responding, I been into the Stamp for only a couple of months
    and even I'm deep into the reference book every day I do appreciate ideas from all of you
    out there!

    Kjell Romma, Norway
Sign In or Register to comment.