# Constants

Suppose you’re working on a program called "Three Cheers" that flashes LEDs, makes hooting sounds, and activates a motor that crashes cymbals together, all in sets of three. A portion of your PBASIC program might contain something like:

      FOR counter = 1 TO 3
        GOSUB Make_Cheers
      NEXT

      FOR counter = 1 TO 3
        GOSUB Blink_LEDs
      NEXT

      FOR counter = 1 TO 3
        GOSUB Crash_Cymbals
      NEXT

The numbers 1 and 3 in the code above are called constants. They are constants because, while the program is running, nothing can happen to change those numbers. This distinguishes constants from variables, which can change while the program is running.

PBASIC allows you to use several numbering systems. By default, it assumes that numbers are in decimal (base 10), our everyday system of numbers. But you can also use binary and hexadecimal (hex) numbers by identifying them with prefixes. And PBASIC will automatically convert quoted text into the corresponding ASCII code(s). For example:

99 | decimal
%1010 | binary
$FE | hex
"A" | ASCII code for A (65)

You can assign names to constants in a similar fashion to how variables are declared. On a BS1, it is identical to variable declarations and on the other BASIC Stamp modelss you use the CON directive. Here is the syntax:

    SYMBOL  Name    = ConstantValue

    Name    CON     ConstantValue

Once created, named constants may be used in place of the numbers they represent. For example:

    SYMBOL  Cheers  = 3

    SYMBOL  idx     = B2

    Main:
      FOR idx = 1 TO Cheers
        GOSUB Make_Cheers
      NEXT

    Cheers  CON     3

    idx     VAR     Nib

    Main:
      FOR idx = 1 TO Cheers
        GOSUB Make_Cheers
      NEXT

That code would work exactly the same as the previous FOR...NEXT loops. The editor software would substitute the number 3 for the constant named Cheers throughout your program. Like variable names, labels and instructions, constant names are not case sensitive; CHEERS, and ChEErs would all be processed as identical to Cheers.

Refer to the Elements of PBASIC Style for suggested guidelines on naming constant values.

Using named constants does not increase the amount of code downloaded to the BASIC Stamp, and it often improves the clarity of the program. Weeks after a program is written, you may not remember what a particular number was supposed to represent—using a name may jog your memory (or simplify the detective work needed to figure it out).

Named constants also have another benefit. Suppose the "Three Cheers" program had to be upgraded to "Five Cheers." In the original example you would have to change all of the 3s to 5s. Search and replace would help, but you might accidentally change some 3s that weren't numbers of cheers, too. However, if you made smart use of a named constant, all you would have to do is change 3 to 5 in one place, the constant's declaration:

    SYMBOL  Cheers  = 5

    Cheers  CON     5

Now, assuming that you used the constant cheers wherever your program needed 'the number of cheers,' your upgrade would be done.

On the BS2-family you can take this idea a step further by defining constants with expressions; groups of math and/or logic operations that the editor software solves (evaluates) at compile time (the time right after you start the download and before the BASIC Stamp starts running your program). For example, suppose the "Cheers" program also controls a pump to fill glasses with champagne. Perhaps the number of glasses to fill is always twice the number of cheers, minus 1 (another constant):

    Cheers  CON     5                       ' # of cheers
    Glasses CON     Cheers * 2 - 1          ' # of glasses

As you can see, one constant can be defined in terms of another. That is, the number glasses depends on the number cheers.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012