## Sunday, March 20, 2011

### Basic Stamp program

At first I was just programming the Basic Stamp board to draw pictures. I very soon ran into limitations with that. This board only has 128 bytes of program space so the program had to be very short. Also i needed floating point math and trig functions for some of the things I wanted to draw. I added serial in and out lines to the board so I could talk to it from the PC. Then I just use the board as a driver for the motors and do all the calculating of the picture in the PC. I plan to replace the Basic Stamp board with an Arduino so I can put all the routines in the board and not need a computer connected to draw pictures.
Here is the program I ended up with in the board to talk to the computer. This lets me send a character, 0 through 8 to the board to move the pen one step in any of 8 directions. 0 moves it up, 1 moves it up and to the right, 2 moves it to the right, etc. Then I right programs in Quick Basic to draw my pictures.

' {\$STAMP BS1}
' {\$PBASIC 1.0}
' left stepper motor connected to P6 and P7
' right one connected TO P3 and P4
' both motors move cw one turn and then ccw one turn
'ccw on left motor winds it up
'cw on right motor winds it up
'00 01 11 10 00 turns cw
'0000000

SYMBOL counter2 = W1
SYMBOL counter1 = W0
SYMBOL left_real = B10 'phase of the motor, 1,2,3,4
SYMBOL right_real = B11
SYMBOL accumulator = B9
SYMBOL left_grey = B8 'grey code of the motor, 0,1,3,2
SYMBOL right_grey = B7
SYMBOL left_dir = B6 '1 for cw, -1 for ccw
SYMBOL right_dir = B5
SYMBOL speed = B4

speed = 0 'speed is how long to wait between steps
DIRS = 254 'set all pins but P0 to outputs
left_real = 1
right_real = 1

'pause 2 seconds to give time to position motors
PAUSE 2000

PINS = 0
SEROUT 1, N2400, ("START",13)

start:
SERIN 0, N2400, #counter2

BRANCH counter2, (up, up_right, right, down_right, down, down_left, left, up_left)
SEROUT 1, N2400, (#counter2, 13)
GOTO start

up:
left_dir=-1 ' wind up left motor
right_dir = 1 'wind up right motor
GOSUB move
GOTO start

up_right:
left_dir = 0 'don't move left motor
right_dir = 1 'wind up right motor
GOSUB move
GOTO start

right:
left_dir = 1 'unwind left motor
right_dir = 1 ' wind up right motor
GOSUB move
GOTO start

down_right:
left_dir = 1 'unwind left motor
right_dir = 0  'don't move right motor
GOSUB move
GOTO start

down:
left_dir = 1  'unwind left motor
right_dir = -1  'unwind right motor
GOSUB move
GOTO start

down_left:
left_dir = 0  'don't move left motor
right_dir = -1  'unwind right motor
GOSUB move
GOTO start

left:
left_dir = -1  'wind up left motor
right_dir = -1  'wind up right motor
GOSUB move
GOTO start

up_left:
left_dir = -1  'wind up left motor
right_dir = 0  'don't move right motor
GOSUB move
GOTO start

'GOTO start
END

move:

left_motor:
left_real = left_real + left_dir
IF left_real < 5 THEN left1
left_real=1
left1:
IF left_real > 0 THEN left2
left_real = 4
left2:
IF left_real <> 1 THEN left3
left_grey = 0
left3:
IF left_real <> 2 THEN left4
left_grey = 1
left4:
IF left_real <> 3 THEN left5
left_grey = 3
left5:
IF left_real <>4 THEN left6
left_grey = 2
left6:
'RETURN

right_motor:
right_real=right_real + right_dir
IF right_real < 5 THEN right1
right_real=1
right1:
IF right_real > 0 THEN right2
right_real = 4
right2:
IF right_real <> 1 THEN right3
right_grey = 0
right3:
IF right_real <> 2 THEN right4
right_grey = 1
right4:
IF right_real <> 3 THEN right5
right_grey = 3
right5:
IF right_real <>4 THEN right6
right_grey = 2
right6:
'RETURN

accumulator = left_grey * 8 + right_grey
accumulator = accumulator * 8
PINS = accumulator
PAUSE speed
SEROUT 1, N2400, (#counter2, 13)

RETURN