Home Chapter 7 BS2 Introducing a new command FOR NEXT and VAR

Site Search


Chinese (Simplified) French German Italian Portuguese Russian Spanish
BS2 Introducing a new command FOR NEXT and VAR

Having the BASIC Stamp count activity is another important way to create conditional behaviors. In this next program you will be using a variable and storing the number of times a button is pressed to make decisions.

In order to use the FOR NEXT LOOP you must be able to assign a variable (which is a register in ram) changing values to keep track what the count is.

The command for creating a variable is VAR.

Remember, variables in bold should be in mixed-case and start with an uppercase letter such as Bit, Nib, Byte or Word.

Variable names should use lower-case and upper case mixed letters to offer yourself descriptive variable names such as: light, ledOut, motor etc.

light VAR Bit ‘a bit (1 bit) permits either o or 1
LedOut VAR Nib ‘a nible (4 bits) permits variables between 0-15
InfrSense VAR ‘Byte ‘a byte (8 bits) permits variable between 0 - 255
Motor VAR Word ‘ a word (16 bits) permits variables between 0-65535

The bits, nibble, bytes and words are all based on binary logic. If you can imagine a one-bit system in binary, you can have two possible states. The bit can either be high or low.

If you have two bits, then you can imagine that you have four different possibilities for storing information. These are bit 1 is high and bit two is high which is one possible state of the pins, Now bit 1 is low and bit two is high another possible state of the pins. Now bit 1 is high and bit two is low another possible state of the pins. Now both bits are low which is the fourth and final permutation that the pins can be in. Therefore, a two-bit system would allow four possible different states of two pins. Now imagine 4 pins and the permutations possible with the four pins. The possible states are 16 different possibilities. Now imagine an 8 pin system or a byte and you can calculate that this would allow 256 different possibilities of arranging these pins in different permutations as would a 16 bit squared allow 65536 different arrangements of these 16 pins in either a high or low states.


The syntax for a FOR NEXT LOOP is:
FOR variable, _1, argument_2, {optional argument}
Therefore, the program below would count down 10 times, causing the LED to blink 10 times and then stop.


' {$STAMP BS2}
' {$PBASIC 2.5}

X VAR Word

FOR x = 1 to 10







This FOR….NEXT loop command can also be used to count events such as pressing a switch. This is valuable as it will allow you to make conditional decisions based on how many times a switch is activated.

Enter the program below.

In this program a conditional branching to a subroutine is allowing you to count the number of times you press the switch on the board and each time you do, the LED attached to pin 0 is turned on an off.


' {$STAMP BS2}
' {$PBASIC 2.5}

Total VAR Word      'create a variable named "total"

X VAR Word           'create a variable named "x"

INPUT 15               'make pin 15 an input

OUTPUT 0             'make pin 0 an output



Look4data:             'start main program

LOW 0                   'turn led off

IF IN15 = 0 THEN add 'look at pin 15 if it is equal to 0 then goto subroutine "add"


GOTO look4data     'return to top of program

Add:                     'start subroutine "add"

FOR total = 1 TO x 'this will count the number of
                            ‘times the button is pressed

HIGH 0                  'turn led on

PAUSE 500            'keep led on for .5 seconds

LOW 0                  'turn led off

PAUSE 200            'keep led off for .2 seconds

NEXT                    ‘go to top of "add" once for each time button is pressed

IF (x=5) THEN take_5

X=x+1                   'add value of one for each time
                            ‘button was pressed

GOTO look4data     'go back to main program


FOR x=1 TO 20
GOTO look4data