PDA

View Full Version : Using AND OR operators in combination



plyrathrt
- 13th February 2011, 20:18
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?

shahidali55
- 14th February 2011, 05:35
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).

plyrathrt
- 16th February 2011, 05:09
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)

HenrikOlsson
- 16th February 2011, 06:35
Hi,
Have you tried

(Y >= 0 and BUTTON1 = BUTTONPRESS) OR (Y = 1 AND BUTTON2 = BUTTONPRESS) OR (Y > 2 AND BUTTON3 = BUTTONPRESS)
or perhaps

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

cncmachineguy
- 16th February 2011, 10:01
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.