Name:
if pin
Syntax:

Single line options:

IF pin XX ?? value {AND/OR variable ?? value ...} THEN label

IF pin XX ?? value {AND/OR variable ?? value ...} THEN GOTO label

IF pin XX ?? value {AND/OR variable ?? value ...} THEN GOSUB label

IF pin XX ?? value {AND/OR variable ?? value ...} THEN EXIT

pin XX - the number of the pin to check.

Value - is a variable/constant (usually either 0 or 1).

?? - comparison operator can be any of the following conditions:

  = equal to
  is equal to
  <> not equal to
  != not equal to
  > greater than
  < less than

Multiple line/block structured options:

IF pin XX ?? value {AND/OR pin XX ?? value ...} THEN

   {code}

ELSEIF pin XX ?? value {AND/OR pin XX ?? value ...} THEN

   {code}

ELSE

   {code}

ENDIF

pin XX - the number of the pin to check.

Value - is a variable/constant (usually either 0 or 1).

?? - comparison operator can be any of the following conditions:

  = equal to
  is equal to
  <> not equal to
  != not equal to
  > greater than
  < less than
Description:

When using inputs the input variable (pin1, pin2 etc) must be used (not the actual pin name 1, 2 etc.) i.e. the line must read 'if pin1 = 1 then...', not 'if 1 = 1 then...'

Some PICAXE parts have additional inputs on porta and portc.  To read the state of PORTA and PORTC pins on the older 28X/X1 parts, they keyword PORTA or PORTC is inserted after IF to redirect the whole line to the desired port. For newer parts use the direct PORT.PIN notation instead e.g. if pinC.1 = 1 then....

When using PORTA or PORTC keywords, it is possible to use AND and OR within the command, but all pins tested will be on the same port, it is not possible to mix ports within one line.

The if...then command only checks an input at the time the command is processed. Therefore it is normal to put the if...then command within a program loop that regularly scans the input. For details on how to permanently scan for an input condition using interrupts see the 'setint' command.

To read the whole input port at once the variable 'pins' can be used if pins = %10101010 then gosub label. To read the whole input port and mask individual inputs (e.g. 6 and 7) let b1 = pins & %11000000,  if b1 = %11000000 then gosub label.

The words is (=), on (1) and off (0) can also be used with younger students.

Applies To:
All
See Also:
Related Create:
Share:
Print:

Flash an LED

Flash an LED when an input goes high

Code Example:
main:	if pin0 = 1 then flsh	; jump to flsh if pin0 is high
	goto main		; else loop back to start

flsh:	high 1			; switch on output 1
	pause 5000		; wait 5 seconds
	low 1			; switch off output 1
	goto main		; loop back to start
Copy Code Submit an Example

Exit a loop

Exit a loop when an input pin goes high

Code Example:
	do
	  if pinC.0 = 1 then exit	; exit if pinC.0 is high
	loop
Copy Code Submit an Example

Using porta

Checking a porta input within a loop

Code Example:
main: 	if porta pin0 = 1 then flsh	; jump to flsh if pin0 is high
	goto main 			; else loop back to start

flsh:	high 1 				; switch on output 1
	pause 5000 			; wait 5 seconds
	low 1 				; switch off output 1
	goto main 			; loop back to start
Copy Code Submit an Example

Gates

Model of various logic gates

Code Example:
;2 input AND gate 
if pinC.1 = 1 and pinC.2 = 1 then gosub label

;3 input AND gate 
if pinC.0 =1 and pinC.1 =1 and pinC.2 = 1 then gosub label 

;2 input OR gate 
if pinC.1 =1 or pinC.2 =1 then gosub label 

;analogue value between certain values 
readadc 1,b1 
if b1 >= 100 and b1 <= 200 then gosub label 

;To read the whole input port at once the variable 'pins' can be used 
if pins = %10101010 then gosub label 

;To read the whole input port and mask individual inputs (e.g. 6 and 7) 
let b1 = pins & %11000000 
if b1 = %11000000 then gosub label 

Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Login to leave a comment.