conditional 'and' statement and PE6.0.8.0 syntax error

DBoyd

New Member
I have read that PE 6.0.8.0 fixed a 'conditional and' bug but I'm still getting a syntax error on the following code.

if b11 = 0 and
b12 = 6 and
b13 = 0 and
b14 = 0 then
switchon pinc.0
endif

I can't download the code (from where I am now) to check it's actual functionality. I will check it elsewhere, later, but want everything correct before then.
Can anyone see a problem with it?
As per the manual, I've tried 'then goto' and 'then gosub' but neither works.
If this code is correct, will someone let the Picaxe software guru's know of the bug?

Thanks.

DBoyd
 

westaust55

Moderator
Try with:
Code:
IF b11 = 0 and b12 = 6 and b13 = 0 and b14 = 0 then 
    SWITCHON C.0 
ENDIF
which is also equivalent to:
Code:
IF b11 = 0 and b12 = 6 and b13 = 0 and b14 = 0 then 
    HIGH C.0 
ENDIF
the parameter pinC.0 is typically used as part of a conditional test and for pin input status in calculations. For example
IF pinC.0 = 1 then <do something>.

To control an individual IO pin as an output use just the Port.Pin designation with commands such as HIGH, LOW, etc .
For example HIGH C.0
 
Last edited:

Goeytex

Senior Member
[Can anyone see a problem with it? ]

Yes. The conditional statement needs to be on one line as WestAust demonstrated. However a little known
but very useful feature feature is the "line continuation character" This is the underscore character _.

IF you want to break it up into separate lines, it can be done like this: Note the use of the line continuation character.

Code:
if b11 = 0 and _
   b12 = 6 and _
   b13 = 0 and _
   b14 = 0 then
      switchon C.0
endif
But IMO the preferred and most common method would be all on the same line UNLESS the line is so long that it wont fit on the page.

"Switch on /off " is a pseudo command provided for the the kiddies in grade school.
Most of the big boys use "high" or "low".

When a Pin is being used as an output to turn something on or off (switchon/off, high, low, pulsout, etc) , do not put "Pin" before the port.pin constant. Just use "C.0".
"PinC.0" is used when the pin is an input and the code is testing the state of the Pin. eg. "If pinc.2 = 0 then goto main"

I can't tell you why your gosub or goto would not work, but I suspect it was related to a syntax error. Here is how I would write your example using a Gosub.

Code:
[color=Navy]#Picaxe [/color][color=Black]20X2  [/color][color=Green]'This directive tells folks what chip is used [/color]

[color=Blue]Symbol RED_LED [/color][color=DarkCyan]= [/color][color=Blue]B.0  [/color][color=Green]'Symbols make code easier to read 

'Note: Commenting Code is necessary for others to 
'      understand what you are trying to do.  [/color]

[color=Black]MAIN: [/color][color=Green]'This is the main loop [/color]
[color=Blue]Do
     If [/color][color=Purple]b11 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]b12 [/color][color=DarkCyan]= [/color][color=Navy]6 [/color][color=DarkCyan]and [/color][color=Purple]b13 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]b14 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then
         Gosub [/color][color=Black]LED_ON
     [/color][color=Blue]Else
          gosub [/color][color=Black]LED_OFF
     [/color][color=Blue]Endif

Loop[/color]

[color=Green]' ======= Sub routines start here ======== [/color]

[color=Black]LED_ON:
  [/color][color=Blue]High RED_LED    [/color][color=Green]'Turn the Led On [/color]
[color=Blue]Return[/color]

[color=Black]LED_OFF:
  [/color][color=Blue]Low RED_LED     [/color][color=Green]'Turn the LED OFF[/color]
[color=Blue]Return [/color]
 

srnet

Senior Member
[Can anyone see a problem with it? ]IF you want to break it up into separate lines, it can be done like this: Note the use of the line continuation character.

Code:
if b11 = 0 and _
   b12 = 6 and _
   b13 = 0 and _
   b14 = 0 then
      switchon C.0
endif
But IMO the preferred and most common method would be all on the same line UNLESS the line is so long that it wont fit on the page.
I would agree, whilst the use of the 'line continuation character' may work, it only obscures the meaning of the code.

Code:
if b11 = 0 and b12 = 6 and b13 = 0 and b14 = 0 then
Is a lot more readable.

I will now forget the use of the 'line continuation character'
 

geoff07

Senior Member
even more readable would be to use symbols for the variables and maybe even the constants so the code could be read meaningfully. Then you might remember what it does when you come back to it in future.

e.g.

if door = closed and lock = secure and light = off and time = bedtime then
call gotosleep
endif
 

westaust55

Moderator
From PICAXE Manual 2: (V7.9.1 page 111)
The words is (=), on (1) and off (0) can also be used with younger students.

Symbols are pre defined for ON and OFF (as they are for CR and LF).
Therefore such as conditional test can become even more &#8220;English&#8221; like in its reading as:

Code:
IF door IS closed AND lock IS secure AND light IS off AND time IS bedtime THEN
    GOSUB gotosleep
ENDIF
Capitals for program keywords is a personal preference
 
Top