If and or then question

newplumber

Senior Member
Hi every1
I was curious on the If statements like how many times is possible to use "If...and...and...or...and...then"
the reason is because when i using my two switches i used this code

Code:
dirsc = %00000000

Start: 
  If pinc.1 = 1 and pinc.0 = 0 OR pinc.1 = 0 and pinc.0 = 1 then 
    sertxd (" one button is pushed ",cr,lf) 
  elseif  pinc.1 = 0 and pinc.0 = 0 then 
    sertxd (" no button is pushed ",cr,lf) 
 Endif 
Goto start
the code will not read pinc.0 = 1 when pinc.0 button is pushed to read high/1
so as always I programmed it wrong and I suppose you can not program it that way
using IF...and...OR...and...then?
So I used this code and broke up the two ANDS which works perfectly
Code:
dirsc = %00000000

Start: 
  If pinc.1 = 1 and pinc.0 = 0 then
    sertxd (" one button is pushed ",cr,lf) 
 Elseif  pinc.1 = 0 and pinc.0 = 1 then 
     sertxd (" one button is pushed ",cr,lf) 
  Elseif  pinc.1 = 0 and pinc.0 = 0 then 
    sertxd (" no button is pushed ",cr,lf) 
 Endif 
Goto start
Another simple question is can you use IF..X=X..AND..X=X..AND..X=X..AND..X=X..forever....THEN ?
your friend
 

hippy

Technical Support
Staff member
I was curious on the If statements like how many times is possible to use "If...and...and...or...and...then"
Never is probably the most correct answer. Mixing AND and OR inevitably leads to results other than expected. The best advice is to not mix AND and OR in a single command.

In the example you give you could use -

Code:
  b0 = pinC.1 + pinC.0
  Select Case b0
    Case 0 : sertxd (" no button is pushed ",cr,lf)
    Case 1 : sertxd (" one button is pushed ",cr,lf) 
    Else   : sertxd (" both buttons pushed ",cr,lf)
 End Select
Or, possibly of more use in a real program where you might only include the %01 and %10 options -

Code:
  b0 = pinC.1 * 2 + pinC.0
  Select Case b0
    Case %00 : sertxd (" no button is pushed ",cr,lf)
    Case %01 : sertxd (" C.0 button is pushed ",cr,lf) 
    Case %10 : sertxd (" C.1 button is pushed ",cr,lf) 
    Else     : sertxd (" both buttons pushed ",cr,lf)
 End Select
 

The bear

Senior Member
I believe the rule of thumb is, "Don't use AND & OR on the same line".

Good luck, bear..

Darn, hippy came up with a proper answer, whilst typing.
 

hippy

Technical Support
Staff member
Another simple question is can you use IF..X=X..AND..X=X..AND..X=X..AND..X=X..forever....THEN ?
On that one yes. As many AND or OR as you want until your line gets too long.

If looking for one and only one button pushed the above SELECT CASE example can be useful ...

Code:
b0 = pinC.3 *2+ pinC.2 *2+ pinC.1
Select Case b0
  Case %001 : SerTxd( "Only C.1 pushed" )
  Case %010 : SerTxd( "Only C.2 pushed" )
  Case %100 : SerTxd( "Only C.3 pushed" )
End Select
 

newplumber

Senior Member
@ Texas thanks so in simple terms its over my head what its doing so don't do it :)

@ hippy thanks I'll write that down "no AND ....OR...in same line"
plus good examples again on CASE another code for me to practice

@the bear ...you have to be fast to beat hippys reply :) ..maybe I can ask at 2am london time but thanks
 

newplumber

Senior Member
@ hippy I'm not a math person but in you code post#5 code
Code:
b0 = pinC.3 *2+ pinC.2 *2+ pinC.1
Select Case b0
  Case %001 : SerTxd( "Only C.1 pushed" )
  Case %010 : SerTxd( "Only C.2 pushed" )
  Case %100 : SerTxd( "Only C.3 pushed" )
End Select
shouldn't it be
Code:
b0 = pinC.3 *[B]4[/B]+ pinC.2 *2+ pinC.1
it just works to get pinc.3 to get %100 for me in my calculations but maybe picaxe is doing something different


UPDATE: never mind my calculator is way off
it was on scientific mode and went biszerk
 
Last edited:

hippy

Technical Support
Staff member
b0 = pinC.3 *2+ pinC.2 *2+ pinC.1

shouldn't it be

b0 = pinC.3 *4+ pinC.2 *2+ pinC.1
No but it's easy to get caught out. It would be that way if traditional maths precedence applied but it doesn't for PICAXE; it's strictly left to right ...

= pinC.3 * 2 + pinC.2 * 2 + pinC.1

= ( ( ( pinC.3 * 2 ) + pinC.2 ) * 2 ) + pinC.1

= ( pinC.3 * 2 * 2 ) + ( pinC.2 * 2 ) + pinC.1

which gives the effective *4 as *2*2
 

hippy

Technical Support
Staff member
Two other ways to make things clearer with -

Code:
b0 = pinC.3 * 2 + pinC.2 * 2 + pinC.1
The first with #DEFINE -

Code:
#Define ALSO * 2 +

b0 = pinC.3 ALSO pinC.2 ALSO pinC.1
Or by setting bits in b0 ...

Code:
b0 = 0
bit0 = pinC.1
bit1 = pinC.2
bit2 = pinC.3
And, because of where the bits are one could do -

Code:
b0 = pinsC / 2 & %111
Code:
b0 = pinsC >> 1 & %111
 

newplumber

Senior Member
Yes your right I guess in windows calculator using scientific mode
it was adding
(1 * 2) + (0*2) + 0 = 2
but in standard mode it adds how picaxe does
1*2 + 0 * 2 + 0 = 4
example is if only pinc.3 is pushed/1
but I do like your code will come in handy
btw the reason I was using windows calculator scientific mode is because I was trying to become a scientist :)
 

newplumber

Senior Member
Thats another cool way
Code:
b0 = 0
bit0 = pinC.1
bit1 = pinC.2
bit2 = pinC.3
Sometimes I feel like the snow flake still flying around the iceberg = picaxe coding
 

newplumber

Senior Member
I don't know I will hook up a board and try that too
IF is a powerful word
I do like the picaxe sim but its so much better using a real chip for me anyways
 

newplumber

Senior Member
@Lbenson Yes funny how simple that code is and works perfect
same with hippys codes ...short ..smart...and ERRless
 

newplumber

Senior Member
Can some help me explain this line

Code:
#Picaxe 20M2 
#NO_DATA
#terminal 4800
START:
B0 = 0 

bit0 = PINC.1
bit1 = PINC.2
bit2 = PINC.3
[B][SIZE=4]B0 = pinsC / 2& %111 [/SIZE] [/B]
Sertxd (" B0 = ",#B0,cr,lf) 
END
I understand B0 = PINSC /2
but what does "& %111" do to B0? I tried to simulate it but can not split it up to see change
 

lbenson

Senior Member
That statement is equivalent to the three above it.

First the values of all portC input pins are assigned to b0. "/2" shifts them right (putting the value of pinC.1 in bit0). ANDing (with "&") %111 causes bit3-7 to be zeroed, bit0-2 to retain whatever values they already had.

For instance, %01110101 & %111 will result in %00000101.

It should simulate.

A bitwise "logical AND" of 1 and 1 results in 1; of 1 and 0 (in any order) results in 0.
 
Last edited:

newplumber

Senior Member
Thanks Lance Your fast ...too fast maybe ...I have to have a few minutes to download what you just explained

Okay I am getting it I think (there is hope)

so thats why its & %111
and not & %000

so B0 = %11111111 & %111

will make B0 = %00000111

Okay I need use the sim for a while see if I can make sense out of it
 
Last edited:

lbenson

Senior Member
Yes, I think you've got it. Your example is correct. It might make things more clear if all the bits were included: %11111111 & %00000111 yields %00000111.

Next up, OR: $10101010 | $00110011 -> ?

Then XOR: $10101010 ^ $00110011 -> ?

Page 25 in Manual 2 (if I'm current) has a list of logical operators, some of which I've never heard of, much less used: XNOR, ANDNOT, ORNOT.

I suspect I have, with more roundabout logic, effectively performed some of those operations.
 

newplumber

Senior Member
Okay I have all 3 push button switches wired to a 20m2
and using this code
Code:
START:
B0 = 0 

bit0 = PINC.0 
bit1 = PINC.1
bit2 = PINC.2
SerTxd( "BO before  & %111 = ",#B0 ,cr,lf)
b0 = pinsC / 2 & %111
SerTxd( "BO after   & %111 = ",#B0 ,cr,lf)

pause 3000 
GOTO START
when only PinC.0 is pressed it shows B0 = 1 in sertxd (B0 before ...
but shows B0 = 0 in sertxd (B0 after...
same as if no buttons are pressed ...wonder if I have something wrong with the code


UPDATE:
okay like always there is a reason why hippy made the switch pins
bit0 = PINC.1
bit1 = PINC.2
bit2 = PINC.3

I moved the switch pins to that order and it works like it should


ya about XNOR, ANDNOT, ORNOT?? is getting to complicated for my brain but still in the back of it someday to understand it all
 
Last edited:

hippy

Technical Support
Staff member
bit0 = PINC.1
bit1 = PINC.2
bit2 = PINC.3

is the same as b0 = pinsC / 2 & %1111

bit0 = PINC.0
bit1 = PINC.1
bit2 = PINC.2

Is the same as b0 = pinsC & %111

Code:
x AND    y   =   x AND y
x NAND   y   =   NOT ( x AND y )
x ANDNOT y   =   x AND ( NOT y )
Code:
OR    0 0 = 0   AND    0 0 = 0   XOR    0 0 = 0
      0 1 = 1          0 1 = 0          0 1 = 1
      1 0 = 1          1 0 = 0          1 0 = 1
      1 1 = 1          1 1 = 1          1 1 = 0
Code:
NOR   0 0 = 1   NAND   0 0 = 1   XNOR   0 0 = 1
      0 1 = 0          0 1 = 1          0 1 = 0
      1 0 = 0          1 0 = 1          1 0 = 0
      1 1 = 0          1 1 = 0          1 1 = 1
Code:
ORNOT 0 0 = 1   ANDNOT 0 0 = 0   XORNOT 0 0 = 1
      0 1 = 0          0 1 = 0          0 1 = 0
      1 0 = 1          1 0 = 1          1 0 = 0
      1 1 = 1          1 1 = 0          1 1 = 1
 

newplumber

Senior Member
Thanks Hippy
just to make it simple and after trying many different tests I like the code
Code:
bit0 = PINC.0
bit1 = PINC.1
bit2 = PINC.2

Is the same as b0 = pinsC & %111
with out dividing and then it makes it perfect for me to figure out what buttons are pressed,
in just numbers but its also awesome you use "& %111" because I tried using just
Code:
bit0 = PINC.0
bit1 = PINC.1
bit2 = PINC.2

b0 = pinsC
and it was showing other pins (example c.3 - c.7) that did not have pulldown resistors were reading high which in turn
makes it unuseable unless you place all c.X pins with pull down resistors

Another question is how can I read B0 in binary on sertxd I guess i didn't see anywhere in the manual
and
Code:
sertxd ( "BO = ", %BO,cr ,lf)
wont work
 

BESQUEUT

Senior Member
Code:
[color=Purple]b0[/color][color=DarkCyan]=[/color][color=Navy]7[/color]
[color=Blue]sertxd ( [/color][color=Red]"BO = "[/color][color=Black], #[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Blue]cr [/color][color=Black],[/color][color=Blue]lf)[/color]
Give
BO = 00000111
 

newplumber

Senior Member
thanks Besqueut

I was hoping it was genius way like some different symbol like using #B0 for dec
and for binary like
Code:
sertxd ("show B0 in binary = ",#$%&*B0,cr,lf)
or like always I'm thinking like a rock :)
 

newplumber

Senior Member
Well texas when I am in code "MODE" I put tape over the O (oh) and a bright LED lit up on the (ZERO)
seems to keep me from making to many mistakes :)
 

BESQUEUT

Senior Member
thanks Besqueut I was hoping it was genius way like...
Sorry for not being as genius as Hippy (or many others...) but it works, and I do not know better way...
Well texas when I am in code "MODE" I put tape over the O (oh) and a bright LED lit up on the (ZERO)
seems to keep me from making to many mistakes :)
As in #22 post, by default, I use small letters, for example b0=7
So if I mistakenly write bo, I can see it immediatly... :eek:
 

newplumber

Senior Member
Ohh your a pro Besqueut and its awesome learning from you and all the others
As in #22 post, by default, I use small letters, for example b0=7
see only pro's think of awesome stuff like that and thanks good info
 

techElder

Well-known member
Ok, then I'll reveal that I hardly ever use a "0" in my programming.

One of the first symbols I define is "symbol zero = 0".

Then I use the word "zero" throughout my program.
 
Top