View Full Version : IF... THEN... ELSE syntax help needed.
searthur
02-16-2012, 04:23 PM
I’m looking for some help on the “IF… THEN… ELSE” syntax. I’m trying to look at the status of three input pins and then change some output pins if a certain condition exists. Below is my source file. Can anyone tell me what I’m doing wrong?
Main:
IF IN5=0 THEN GPOHI
ELSEIF
IN6=0 THEN GPOHI
ELSEIF
IN7=1 THEN GPOHI
ELSE GPOLO
ENDIF
GOTO Main
END
GPOHI:
HIGH 15 'Energize relay 1
HIGH 14 'Energize relay 2
HIGH 13 'Energize relay 3
GOTO Main
GPOLO:
LOW 15 'Relax relay 1
LOW 14 'Relax relay 2
LOW 13 'Relax relay 3
GOTO Main
Looks good. You are using PBASIC 2.5, not 2.0?
searthur
02-16-2012, 04:34 PM
I'm running 2.5.2 When I do a syntax check I get "expected ":" or end-of-line error just after the IN6=0. So is it ok to have more than one ELSEIF like I'm trying to do above?
IF IN6=0
IF IN7=1
You left out two IF statements.
Ron Czapala
02-16-2012, 05:02 PM
I generally avoid ELSEIF, but try:
' {$STAMP BS2}
' {$PBASIC 2.5}
Main:
IF (IN5=0) THEN
GOTO GPOHI
ELSEIF(IN6=0) THEN
GOTO GPOHI
ELSEIF (IN7=1) THEN
GOTO GPOHI
ELSE
GOTO GPOLO
ENDIF
GOTO Main
END
GPOHI:
HIGH 15 'Energize relay 1
HIGH 14 'Energize relay 2
HIGH 13 'Energize relay 3
GOTO Main
GPOLO:
LOW 15 'Relax relay 1
LOW 14 'Relax relay 2
LOW 13 'Relax relay 3
GOTO Main
Main:
IF IN5=0 THEN GPOHI
IF IN6=0 THEN GPOHI
IF IN7=1 THEN GPOHI
GOTO GPOLO
or:
Main:
IF IN5=0 OR IN6=0 OR IN7=1 THEN GPOHI
GOTO GPOLO
Tracy Allen
02-16-2012, 05:15 PM
That particular condition could be written
IF IN5=0 OR IN6=0 OR IN7=1 THEN GOTO GPOHI ELSE GOTO GPOLO
searthur
02-16-2012, 05:16 PM
I get "label already defined" at the second ELSEIF...
searthur
02-16-2012, 05:19 PM
I get "expected a label" after the "THEN" statement.
searthur
02-16-2012, 05:22 PM
What about this (see below). It passes the syntax check and I think will do the same thing.
Main:
IF IN5=0 THEN GPOHI
IF IN6=0 THEN GPOHI
IF IN7=1 THEN GPOHI
GOSUB GPOLO
GOTO Main
END
'************************************************* *****************[ Subroutines ]
'
GPOHI:
HIGH 15 'Energize relay 1
HIGH 14 'Energize relay 2
HIGH 13 'Energize relay 3
GOTO Main
GPOLO:
LOW 15 'Relax relay 1
LOW 14 'Relax relay 2
LOW 13 'Relax relay 3
RETURN
GOTO Main
Sure, that subroutine works, too. You don't need your END or final GOTO MAIN statement, as they'll never execute.
Ron Czapala
02-16-2012, 05:34 PM
Make sure you have the
' {$PBASIC 2.5}
statement if you want to use 2.5
searthur
02-16-2012, 05:35 PM
Thanks erco. This seems to work nicely (see below).
Main:
IF IN5=0 OR IN6=0 OR IN7=1 THEN GPOHI
GOTO GPOLO
GPOHI:
HIGH 15 'Energize relay 1
HIGH 14 'Energize relay 2
HIGH 13 'Energize relay 3
GOTO Main
GPOLO:
LOW 15 'Relax relay 1
LOW 14 'Relax relay 2
LOW 13 'Relax relay 3
GOTO Main
searthur
02-16-2012, 05:37 PM
Thank you everyone for all the assistance. I really appreciate it.