You are here: PBASIC Language Reference > Conditional Compilation

Conditional Compilation

BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon {PBASIC 2.5}

 

 

 

 

In order to facilitate the creation of programs that can be compiled for and downloaded to the connected model of BASIC Stamp2, several conditional compile directives have been added to PBASIC 2.5: #DEFINE, #IF...#THEN...#ELSE, #SELECT...#CASE, and #ERROR. Conditional compile directives are evaluated before the program is compiled, so variables and named constants cannot be referenced within a conditional compilation definition.

#DEFINE

Syntax: #DEFINE Symbol {= Value}

#DEFINE allows the programmer to create custom symbols for use within conditional compilation control structures.

Example:

#DEFINE DebugMode = 1

In the example above, the "= 1" is optional. Note that when the compiler encounters an undefined symbol it will evaluate that symbol as False (0). You can define a symbol as False by removing the definition with a comment character ( ' ) or change its value to zero (recommended)

#IF...#THEN...#ELSE

Syntax:

   #IF Condition #THEN
     Statement(s)
 { #ELSE
     Statement(s) }
   #ENDIF

Function

Evaluate Condition and, if it is True, compile the statement(s) following #THEN, otherwise compile the statements following #ELSE.

Example:

' set Baud for 9600-N81

#IF ($STAMP = BS2sx) OR ($STAMP = BS2p) #THEN
  Baud CON 240
#ELSE
  Baud CON 84
#ENDIF

In this example, the constant Baud is set to match the connected BASIC Stamp. This code will work with the BS2, BS2e, BS2sx, BS2p and BS2pe.

#SELECT...#CASE

Syntax:

   #SELECT Expression
     #CASE Condition(s)
       Statement(s)
   { #CASE Condition(s)
       Statement(s)
     #CASE #ELSE
       Statement(s) }
   #ENDSELECT

Function

Evaluate Expression and then conditionally compile a block of code based on comparison to Condition(s). If no conditions are found True and a #CASE ELSE block is included, the #CASE #ELSE code statements will compiled.

Example:

#SELECT $STAMP
  #CASE BS2, BS2e, BS2sx
    GOSUB LCD_Write
  #CASE #ELSE
    LCDOUT LCDpin, cmd, [char]
#ENDSELECT

#ERROR

Syntax: #ERROR Message

#ERROR allows the programmer to create custom a custom error dialog.

Example:

#DEFINE I2CReady = (($STAMP = BS2p) OR ($STAMP = BS2pe))
#IF NOT I2CReady #THEN
  #ERROR "BS2p or BS2pe is required for this program."
#ENDIF

The example above will halt the program and display the dialog below if you attempt to compile the program and run it on a BASIC Stamp other than the BS2p or BS2pe:

Go to PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/21/2013