error with command: if and or then

MarkLondon

New Member
There is a logic error in the if and/or then command when mixing "AND" with "OR" in the condition with PICAXE Editor 6.0.8.1

The error occurs with a multiple condition of the form: if condition#1 AND condition#2 OR condition#3 then ....
The pdf manual and on-line language guide does not mention any restriction in mixing "AND" with "OR" in the condition. If using this form of condition you may not get the logical outcome you want.

The error is when condition evaluates ( True AND False OR False ) in the if-then.
The situation is the same whether using bit, byte or word variables.
See the example below (with truth table of other cases at the bottom):

Code:
    #picaxe 28X2    

    'note different results for case#1 and case #2
    'evaluating: bit0 and bit1 or bit2
    'expected result for 1,0,0, working left to right: 1 and 0 or 0 => (1 and 0) or 0 => (0 or 0) => 0 
    bit0=1
    bit1=0
    bit2=0
    
    'case #1: evaluate logic outside if-then  ' evaluates -> 0 ,correct
    Sertxd ("case #1=")
    bit3 = bit0 And bit1 Or bit2 
    If bit3=1 Then
        Sertxd ("1")
    Else
        Sertxd("0")
    End if
    
    'case #2: evaluate logic inside if-then  'evaluates  -> 1, error
    Sertxd ("case #2=")
    If bit0=1 And bit1=1 Or bit2=1  Then
        Sertxd ("1")
    Else
        Sertxd("0")
    End if

#rem
This is a truth table for all patterns tested:
bits (0,1,2)     Logic        case#1     case#2: is not if-then
 a b c       |         &     a&b|c_#1  a&b|c_#2
 0 0 0       0         0       0            0       
 1 0 0       1         0       1 (!)        0          
 0 1 0       1         0       0            0       
 1 1 0       1         0       1            1       
 0 0 1       1         0       1            1       
 1 0 1       1         0       1            1       
 0 1 1       1         0       1            1       
 1 1 1       1         1       1            1  
result      OK       OK      Error         OK 

#endrem
The truth table shows the logic for a|b|c and a&b&c conditions in if-then are OK.
The logic for a&b|c gives different results as described, with if-then being incorrect, and giving a|c.
Adding a 4th variable (d) we if we test: a&b&c|d in the if-then we get: (a&b)|d

Conditions of "OR" followed by "AND" e.g. a|b&c work OK
 
Last edited:

Technical

Technical Support
Staff member
Thanks for the feedback, it looks like a PE6 simulator error that will be fixed for the next release.
 
Last edited:

MarkLondon

New Member
Technical,

Agreed, I am finding that the firmware is fine on the 28X2, only simulation is affected.

May be worth changing the thread to state "simulation error ..."
 
Last edited:

Mad Mart Uk

New Member
I am having something similar where if I have something like:

Code:
[color=Black]main:
      [/color][color=Blue]let [/color][color=Purple]dirsA [/color][color=DarkCyan]= [/color][color=Navy]16
      [/color][color=Blue]let [/color][color=Purple]dirsB [/color][color=DarkCyan]= [/color][color=Navy]255
      [/color][color=Blue]let [/color][color=Purple]dirsC [/color][color=DarkCyan]= [/color][color=Navy]255

      [/color][color=Blue]serout B.7[/color][color=Black], [/color][color=Blue]N2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black], [/color][color=Navy]128[/color][color=Black], [/color][color=Red]"*Menu Selected* "[/color][color=Blue])
      serout B.7[/color][color=Black], [/color][color=Blue]N2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black], [/color][color=Navy]192[/color][color=Black], [/color][color=Red]"Please Wait...->"[/color][color=Blue])

Cell_7_18:
      [/color][color=Blue]serout B.7[/color][color=Black], [/color][color=Blue]N2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black], [/color][color=Navy]128[/color][color=Black], [/color][color=Red]"Display| Test   "[/color][color=Blue])
      serout B.7[/color][color=Black], [/color][color=Blue]N2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black], [/color][color=Navy]192[/color][color=Black], [/color][color=Red]" >-Y-< | >-N-<  "[/color][color=Blue])

      pause [/color][color=Navy]1000
      [/color][color=Blue]gosub [/color][color=Black]prc_Yes_No_Key
      [/color][color=Blue]if [/color][color=Purple]varY [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
            goto [/color][color=Black]Cell_7_24
      [/color][color=Blue]end if
      goto [/color][color=Black]Cell_1_11

Cell_7_24:
      [/color][color=Blue]goto [/color][color=Black]Cell_1_11

prc_Yes_No_Key:
      [/color][color=Blue]let [/color][color=Purple]varY [/color][color=DarkCyan]= [/color][color=Navy]0    [/color]
[color=Black]Cell_13_3:
      [/color][color=Blue]if [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_13_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_16_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_19_4
 
            goto [/color][color=Black]Cell_37_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_40_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_43_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_46_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_49_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_52_4
      [/color][color=Blue]end if
      if [/color][color=Purple]pinA.0[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=DarkCyan]AND [/color][color=Purple]pinA.3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then

            goto [/color][color=Black]Cell_55_4
      [/color][color=Blue]end if
      goto [/color][color=Black]Cell_13_3[/color]
and i have selected just a single pin on the A port of the simulator the logic is fine (In both Code and Flowchart)
With more than one pin selected e.g. A.0=1 AND A.1=1 then it will select A.0 and ignore the status of A.1 (I have made sure that the compare is 1 or 0 and not floating, although it does not seem to make any difference.

The same will happen with any combination I tried using pins A.0 > A.3 and it will also choose the 1st pin that is high in the order A.0 to A.3

As a second example if I selected A.0= 1 AND A.1=1 AND A.3=1 AND A.3=1
Where pin A.1 as the first scanned Pin that is HIGH - will always be selected by the compare and jump to the routine for that Pin only.

I've not had a chance to test on an actual device - but this anomaly is appearing so far using 40X2 and 28X2 simulations - Hopefully it is the simulation only as you suggest
 

Technical

Technical Support
Staff member
There is no issue with multiple ANDS, the issue described above is only with with an OR following an AND.

A simple flowchart decision cell with multiple portA inputs selected works fine for us, so the issue is probably elsewhere. Your posted program is incomplete, please attach the whole flowchart file.
 

Mad Mart Uk

New Member
Thanks very much - It is probably me at fault doing something stupid
The flowchart is for a 'simple' menu selection procedure, just to get me moving forward at the moment with a project.
However; here is the requested file:
 

Attachments

hippy

Ex-Staff (retired)
With more than one pin selected e.g. A.0=1 AND A.1=1 then it will select A.0 and ignore the status of A.1
You flowchart simulates as expected for me. With A.0 and A.1 high, A.2 and A.3 low, it proceeds through the "Yes_No Key" decisions before taking the "A.0 & A.1 = [0] Button 1100" commented branch.

Make sure you have the latest version of PE6 and not some older version.
 

Mad Mart Uk

New Member
Thanks Hippy

Nailed it on the head (I should know better)
I am using the Uncompressed MSI for networks version available on the website downloads. This appears to be the older version 6075 "http://www.picaxe.com/downloads/pe6075/PICAXEEditor6.zip where the latest full download is 6082 "http://www.picaxe.com/downloads/pe6082/PICAXEEditor6.exe"? Or have I lost the plot?

If I am right - is there any chance that this Zipped version of download could be updated please, as for various reasons I am unable currently to use the Main installer Version.
 

hippy

Ex-Staff (retired)
I am using the Uncompressed MSI for networks version available on the website downloads. This appears to be the older version 6075
Thanks for spotting that and please accept our apologies that it isn't the latest version. I will refer the issue on.
 

Mad Mart Uk

New Member
Just as a final Note - Updating to 6082 did resolve the issue with Port ANDS as described above using an earlier version of the Picaxe Editor

thank you very much for your assistance.

:cool:
 
Top