How variable if then for 40X2

Vroom

Member
How check if four variables and then code for 40X2, example b0 = 1, b1 = 1, b2 = 0, b3 = 0 when check same time four variables together then code, I tried to find info like 86 pages, manual2. Just show simple code example here, but other long code I make own, not like here so only show example.

dirsA=%11111111

main:
if pinC.1 = 1 then first
if pinC.0 = 1 then second
goto main

first:
b0 = 1
b1 = 1
b2 = 0
b3 = 0
wait 2
goto check

second:
b0 = 0
b1 = 0
b2 = 1
b3 = 1
wait 2
goto check

check:
if b0 = 1, b1 = 1, b2 = 0, b3 = 0 then light1 'something is wrong, how command?
if b0 = 0, b1 = 0, b2 = 1, b3 = 1 then light2 'check 4 variables like 0 or 1 each other then
end

light1:
pinsA=%11110000
wait 2
pinsA=%00000000
b0 = 0
b1 = 0
goto main



light2:
pinsA=%00001111
wait 2
pinsA=%00000000
b2 = 0
b3 = 0
goto main
 
Last edited:

westaust55

Moderator
For you code:

Code:
check:
if b0 = 1, b1 = 1, b2 = 0, b3 = 0 then light1 'something is wrong, how command?
if b0 = 0, b1 = 0, b2 = 1, b3 = 1 then light2 'check 4 variables like 0 or 1 each other then
end
use this:

Code:
check:
if b0 = 1 AND b1 = 1 AND b2 = 0 AND b3 = 0 then light1 
if b0 = 0 AND b1 = 0 AND b2 = 1 AND b3 = 1 then light2 
end
IF you have a lot of such tests you can use some IF THEN ELSE ELDIF structures which can save program space.

For example this saves 10 bytes over having 8 full lines of tests for each variable
Code:
check:
IF b0 = 0 THEN
  if b1 = 0 AND b2 = 0 AND b3 = 0 then light1 
  if b1 = 0 AND b2 = 1 AND b3 = 1 then light1 
  if b1 = 1 AND b2 = 0 AND b3 = 0 then light1 
  if b1 = 1 AND b2 = 1 AND b3 = 1 then light1
ELSE   ; here if b0 <> 0 or use "ELSEIF b0 =1" if b0 can have other values than 0 or 1
  if b1 = 0 AND b2 = 0 AND b3 = 0 then light2 
  if b1 = 0 AND b2 = 1 AND b3 = 1 then light2 
  if b1 = 1 AND b2 = 0 AND b3 = 0 then light2 
  if b1 = 1 AND b2 = 1 AND b3 = 1 then light2 
ENDIF
 
Last edited:

westaust55

Moderator
Thankyou. put AND. 40X2 variable up to B55 correct? other more than B55? or C55 or never?
Byte variables for values are those with the predefined identifiers b0 through to b55 inclusive.

There are no other byte variables available in PICAXE BASIC.

When you mention C55 I think you may be confusing variables and ports.
depending upon the PICAXE chips there can be ports A, B, C, D, and E.
 

Dippy

Moderator
Are these 1s and 0s from , for example, Port.pin switch positions?
If so, could you make them bits rather that bytes?

Then you could shrink the long IF statement down to a single byte comparison.
If that is possible it could save some space and would be quicker.

I'll leave a long explanation to others as I have to go shopping.
 
Top