Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

Programming the BASIC Stamp

 

The BASIC stamp is programmed using a variation of the BASIC programming language. As the name implies, it's a very simple language. To program it, you need a DOS-based PC, a copy of the stamp BASIC compiler (available from Parallax), and a cable to connect the PC to the stamp.

The BASIC compiler is a program for the PC that allows you to write and edit programs for the stamp, then compiles your program into the stamp's machine language and downloads it to the stamp. It connects to the stamp using a serial cable, attached to the PC's serial port (n.b. a BASIC stamp 1 connects to the parallel port with a different kind of cable than the BASIC stamp 2 that we're using.).

Like any good programming language, the main structures of BASIC are if statements, repeat loops, and variables. If statements are used to direct the program's actions based on changing conditions.

	main:
		if in5 = 1 then light
	goto main
	
	light:
		high 9
	goto main
	

Unlike other programming languages, there are no multiple line if-then statements in stamp BASIC. All if-then statements are structured as "If (condition) then (label)"

Repeat loops make the program repeat a command or series of commands a certain number of times.

		For x = 1 to 10
			debug ?x
			y = x * 10
		next
		

There are no "repeat while" or "repeat until" statements in stamp BASIC. To create an infinite repeat loop, use a GOTO statement (see below).

Variables are storage locations in the stamp's memory for changing information. In BASIC, variables have to be declared at the start of the program so the stamp knows how much memory to set aside for them. You declare both the name and the size of the variable (is it a bit, a byte, a word (2 bytes):

		lightVar var bit
		potVar var word
		numberVar var byte
		
Blocks of commands in BASIC are marked off by labels. A label is nothing more than a word with a colon after it. They're the same things as handler names in Lingo:
		doSomething:
			high 3
			low 2
			goto main
	

A GOTO statement jumps to another label in your program and continues execution from there.

Comments (statements that are not executed) in BASIC all start with a single quote ('). When the BASIC compiler sees a single quote, it ignores everything else to the end of the line. You should comment your code well, so you (and anyone else reading it) knows what the program does.

The main thing you want to do is set the stamp into an infinite repeat loop so it's always running. All of your instructions for the program go inside that loop. Any variables or conditions that you need to set up before the program needs to run go before of that loop.

Here's a simple program to make an LED flash on and off indefinitely (assumes the LED is on pin 9) :

		output 9        'sets pin 9 as an output pin
		
		main: 
			high 9       'make the output pin high 
			pause 250    'wait 250/1000ths of a second 
			low 9        'make the output pin low, turning off the light 
			pause 250    'wait 250/1000ths of a second 
		goto main       'go back to the "main" label