The BASIC Stamp 2 microprocessor has a whopping 32 bytes of RAM memory that's available for processing for your electronics project. To use RAM memory in PBASIC, you create variables. A variable is simply a name that refers to a location in RAM.

To create a variable, you list the name you want to use for the variable, followed by the keyword VAR, followed by one of four keywords that indicates the type of the variable you're creating. The following creates a variable named Count, using the variable type BYTE:


There are four choices for the variable type:

  • BYTE — Uses one of the 32 available bytes of RAM and can have a value ranging from 0 to 255. This type of variable is useful for simple counters that don't need to exceed the value 255. If you're creating a timer that will count down 60 seconds, a BYTE variable will do the trick.

  • WORD — Uses two of the 32 available bytes and can have a value ranging from 0 to 65,535. You need to use a WORD variable whenever the value to be stored in the variable is greater than 255. A WORD variable is ideal for holding the length of a delay used by the PAUSE statement.

  • NIB — If you have a very small counter whose value will never exceed 15, you can use a NIB variable, which requires only one-half of one byte of RAM.

  • BIT — Uses just one binary bit. Thus, the BASIC Stamp can squeeze up to eight BIT variables in each of its 32 bytes of available RAM. BIT variables are mostly used to keep track of whether some event has occurred. You could set up a BIT variable to remember whether a user has pressed an input button: the value 0 for No and the value 1 for Yes.

Once you’ve created a variable, you can use it in an assignment statement to assign it a value. For example, this assignment statement assigns the value 500 to a variable named Delay:

Delay = 500

The value on the right side of the equals sign can be an arithmetic calculation. The real power of variable assignments happens when you use variables on the right side of the equals sign. For example, the following statement increases the value of the Delay variable by 10:

Delay = Delay + 10

In this example, the previous value of Delay is increased by 10.

This program uses a variable to change the speed at which the LEDs flash each time the GOTO statement causes the program to loop. As you can see, a variable named Delay is used to provide the number of milliseconds that the PAUSE statement should pause.

Each time through the loop, the value of the Delay variable is increased by 10. Thus, the LEDs flash very fast when the program first starts, but the flashing gets progressively slower as the program loops.

' LED Flasher Program
' Doug Lowe
' July 10, 2011
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' at one-half second intervals.
' This version of the program uses a variable delay.
' {$PBASIC 2.5}
' {$STAMP BS2}
Led1 PIN 0
Led2 PIN 2
Led3 PIN 4
Led4 PIN 6
Led5 PIN 8
Led6 PIN 10
Delay VAR Word
Delay = 10
 HIGH Led1
 HIGH Led2
 HIGH Led3
 HIGH Led4
 HIGH Led5
 HIGH Led6
 PAUSE Delay
 LOW Led1
 LOW Led2
 LOW Led3
 LOW Led4
 LOW Led5
 LOW Led6
 PAUSE Delay
 Delay = Delay + 10
 GOTO Main

One final note about using variables: PBASIC lets you use a variable in a HIGH or LOW statement to indicate which pin should be controlled. For example:

Led = 0

This sequence of statements creates a variable named Led, assigns the value 0 to it, and then uses it in a HIGH statement. The result is that I/O pin 0 is set to HIGH.