Shop OBEX P1 Docs P2 Docs Learn Events
IF... THEN... ELSE syntax help needed. — Parallax Forums

IF... THEN... ELSE syntax help needed.

searthursearthur Posts: 8
edited 2012-02-16 10:37 in General Discussion
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

Comments

  • ercoerco Posts: 20,259
    edited 2012-02-16 09:28
    Looks good. You are using PBASIC 2.5, not 2.0?
  • searthursearthur Posts: 8
    edited 2012-02-16 09:34
    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?
  • ercoerco Posts: 20,259
    edited 2012-02-16 09:56
    IF IN6=0

    IF IN7=1

    You left out two IF statements.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2012-02-16 10:02
    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
    
  • ercoerco Posts: 20,259
    edited 2012-02-16 10:14
    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 AllenTracy Allen Posts: 6,664
    edited 2012-02-16 10:15
    That particular condition could be written
    IF IN5=0 OR IN6=0 OR IN7=1 THEN GOTO GPOHI ELSE GOTO GPOLO
    
  • searthursearthur Posts: 8
    edited 2012-02-16 10:16
    I get "label already defined" at the second ELSEIF...
  • searthursearthur Posts: 8
    edited 2012-02-16 10:19
    I get "expected a label" after the "THEN" statement.
  • searthursearthur Posts: 8
    edited 2012-02-16 10:22
    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
  • ercoerco Posts: 20,259
    edited 2012-02-16 10:27
    Sure, that subroutine works, too. You don't need your END or final GOTO MAIN statement, as they'll never execute.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2012-02-16 10:34
    Make sure you have the

    ' {$PBASIC 2.5}

    statement if you want to use 2.5
  • searthursearthur Posts: 8
    edited 2012-02-16 10:35
    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
  • searthursearthur Posts: 8
    edited 2012-02-16 10:37
    Thank you everyone for all the assistance. I really appreciate it.
Sign In or Register to comment.