Using AND OR operators in combination


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Posts
    42

    Default Using AND OR operators in combination

    Ran in to some issues with code space on my mico and am trying to get more room. The question I have is can you use AND OR in combination together?

    For instance, I have a subroutine which repeats until a couple different actions become true. Right now I have a second subroutine, exactly the same but waiting for different conditions to become true. I would like to combine these two subroutines to one single subroutine to conserve space.

    Here is an example.

    firstroutine:
    repeat
    output led1
    led1 = ledon
    pause 1000
    until (button1 = buttonpress) or (button2 = buttonpress)

    second routine:
    repeat
    output led1
    led1 = ledon
    pause 1000
    until (button3 = buttonpress)

    So, I could assign a variable for the two modes using the two different subroutines but could I write it like this?

    combinedroutine:
    repeat
    output led1
    led1 = ledon
    pause 1000
    until (mode = 1) and (button1 = buttonpress) or (mode = 1) and (button2 = buttonpress) or (mode = 2) and (button3 = buttonpress)

    Do you have to doube (( )) each combination for picbasic to combine the operators together?

  2. #2
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    136


    Did you find this post helpful? Yes | No

    Post Re: Using AND OR operators in combination

    Maybe this will work,
    until ((((mode = 1) and (button1 = buttonpress)) or ((mode = 1) and (button2 = buttonpress))) or ((mode = 2) and (button3 = buttonpress)))
    You could also use a variable to store the intermediate results and finally arrive at the end result.
    Then you use this variable as the until's condition (either true or false).

  3. #3
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Re: Using AND OR operators in combination

    While I couldn't figure out the logic to all the ('s, I tried it anyway but I get "bad expression" when compiling.

    I have tried it many different ways with no luck. I spoke with Micro-engineering labs as well and they looked at the post and said that the way you describe, is the correct way but they were unsure if your "exact" expression would work.

    Any other thoughts? Could it be because my expressions check a variable as well as a pin state?

    This is the 3 expressions I need to break in to individual expression during an UNTIL function. If confusing, I have seperated the 3 expressions below the example.

    EXAMPLE:
    (Y >= 0) and (BUTTON1 = BUTTONPRESS) OR (Y = 1) AND (BUTTON2 = BUTTONPRESS) OR (Y > 2) AND (BUTTON3 = BUTTONPRESS)

    The 3 expressions seperated by the OR
    (Y >= 0) and (BUTTON1 = BUTTONPRESS)
    (Y = 1) AND (BUTTON2 = BUTTONPRESS)
    (Y > 2) AND (BUTTON3 = BUTTONPRESS)

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Using AND OR operators in combination

    Hi,
    Have you tried
    Code:
    (Y >= 0 and BUTTON1 = BUTTONPRESS) OR (Y = 1 AND BUTTON2 = BUTTONPRESS) OR (Y > 2 AND BUTTON3 = BUTTONPRESS)
    or perhaps
    Code:
    ((Y >= 0) and (BUTTON1 = BUTTONPRESS)) OR ((Y = 1) AND (BUTTON2 = BUTTONPRESS)) OR ((Y > 2) AND (BUTTON3 = BUTTONPRESS))

  5. #5
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Using AND OR operators in combination

    I too am not sure why the first example from shahidali55 didn't work. Seems all the () are in place. Of the 2 from Henrick I think the second will be best.

    Having said that, Even if it works, I am not sure you will save code space using it. It will take a lot of ASM to make that statement.

    Another idea may be to go back to your original post and make the common lines a subroutine. this way the pause1000 will only be coded once and you won't have the complicated until expression.

    Of course you will have to verify by looking at the compiled size.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts