Shop OBEX P1 Docs P2 Docs Learn Events
Trouble with Syntax BS2 — Parallax Forums

Trouble with Syntax BS2

KennyKKennyK Posts: 4
edited 2014-08-11 04:42 in BASIC Stamp
Hello,

I am having some trouble where the code does not let me run since it fails the syntax test. I cannot figure out what I am doing wrong.

The compiler keeps telling me that I need an IF before the Else.

Here is part of the code:
' {$STAMP BS2}    'This indicates to use a Basic Stamp 2 module' {$PBASIC 2.5}   ' Use the PBasic 2.5 language, which provides additional commands


' Constants
period     CON 329      ' period    = 330 msec
pw         CON 49       ' pulse width =  50 msec, proper PW is 15-75 ms
shortpw    CON 13       'set the short pulse to 2 ms less than the min pulse width
longpw     CON 77       'set the long pulse to 2 ms longer than the max pulse width
magneton   PIN 0        'magnet sensor is on Pin0
sweephand   PIN 1        'sweep hand input is on Pin1
switch1    PIN 3        'switch to select short pulse
switch2    PIN 4        'switch to select long pulse
switch3    PIN 5        'switch to select missing pulse


'Pin2 is the output line for the RTR pulses


INPUT switch1
INPUT switch2
INPUT switch3
INPUT magneton
INPUT sweephand


' Variables
x     VAR Byte    ' for/next loop counter




LOW   2                    'Set P2 to low to initialize it


Watch:


  IF (magneton = 1 AND sweephand = 1) THEN  Watch       'Loop here until the magnet moves (switch pulls input low when movement detected)
    ELSE Begin                                                                 'magnet moved, so go to pulse output section
  ENDIF

Comments

  • banjobanjo Posts: 443
    edited 2014-08-11 03:31
    I don't know if it matters, but try with a line break before ... {$PBASIC 2.5} on the first row
  • KennyKKennyK Posts: 4
    edited 2014-08-11 03:37
    Sorry, it must have copied funny. Yes it is on its own line already.
  • banjobanjo Posts: 443
    edited 2014-08-11 04:03
    Ok, am at work now so cannot try it on my "Stamp"-computer or easily check the correct syntax for IF-THEN-ELSE.
    Verify that your syntax is correct by checking the built-in help in the editor. Try also temporarily removing the comments from the "IF"-related rows
  • PublisonPublison Posts: 12,366
    edited 2014-08-11 04:06
    You need to include the "INPUT" commands in your "Watch" subroutine,or you will never check for changes.
    ' {$STAMP BS2}' {$PBASIC 2.5}
    
    
    ' Constants
    period     CON 329      ' period    = 330 msec
    pw         CON 49       ' pulse width =  50 msec, proper PW is 15-75 ms
    shortpw    CON 13       'set the short pulse to 2 ms less than the min pulse width
    longpw     CON 77       'set the long pulse to 2 ms longer than the max pulse width
    magneton   PIN 0        'magnet sensor is on Pin0
    sweephand  PIN 1        'sweep hand input is on Pin1
    switch1    PIN 3        'switch to select short pulse
    switch2    PIN 4        'switch to select long pulse
    switch3    PIN 5        'switch to select missing pulse
    
    'Pin2 is the output line for the RTR pulses
    
    
    INPUT switch1
    INPUT switch2
    INPUT switch3
    
    ' Variables
    x     VAR Byte    ' for/next loop counter
    
    
    LOW   2                    'Set P2 to low to initialize it
    
    
    Watch:
    
    
    INPUT magneton
    INPUT sweephand
    IF (magneton = 1) AND (sweephand = 1) THEN
      GOTO Watch       'Loop here until the magnet moves (switch pulls input low when movement detected)
    ELSE
      GOTO SomethingElse        'magnet moved, so go to pulse output section
    ENDIF
    
    
    SomethingElse:
    
  • KennyKKennyK Posts: 4
    edited 2014-08-11 04:33
    That worked, thank you!
  • banjobanjo Posts: 443
    edited 2014-08-11 04:37
    I suppose it was the GOTO that was the main missing part then from syntax point of view?
  • PublisonPublison Posts: 12,366
    edited 2014-08-11 04:40
    banjo wrote: »
    I suppose it was the GOTO that was the main missing part then from syntax point of view?
    Watch:
    
    
    [COLOR=#ff0000]INPUT magneton[/COLOR]
    [COLOR=#ff0000]INPUT sweephand[/COLOR]
    IF (magneton = 1)[COLOR=#ff0000] AND[/COLOR] (sweephand = 1) THEN
      [COLOR=#ff0000]GOTO[/COLOR] Watch       'Loop here until the magnet moves (switch pulls input low when movement detected)
    ELSE
      [COLOR=#ff0000]GOTO[/COLOR] SomethingElse    'Magnet moved, so go TO pulse OUTPUT section
    ENDIF
    
    
    SomethingElse:[COLOR=#ff0000][/COLOR]
    

    The INPUT commands where missing in the Watch subroutine
    The AND was in the parens
    GOTO was missing on both branches
    "Begin" was missing as a subroutine, (I renamed it "SomethingElse"
  • KennyKKennyK Posts: 4
    edited 2014-08-11 04:42
    That is what I assume.

    I initially had the GOTO in there, but it was giving errors (I had it in the wrong spot). When I removed it, I was getting the IF errors.

    Now it downloads to the Stamp.
Sign In or Register to comment.