Wednesday, February 22, 2006

Misc Stamp Programs

Misc Basic Stamp Programs



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

co_ambi VAR Word
cycle_L VAR Word
cycle_R VAR Word
I VAR Word
stride VAR Word
distance VAR Word

motor_R PIN 0
motor_L PIN 1
rc_L PIN 2
rc_R PIN 3
rc_ambi PIN 4
trigger PIN 5
recieve PIN 6

DIRA = %0011
DIRB = %0010

PAUSE 3000

DO
GOSUB charging
GOSUB direction
GOSUB drive
GOSUB avoid
LOOP
END

charging:
HIGH rc_ambi
PAUSE 100
RCTIME rc_ambi, 1, co_ambi
IF co_ambi = 1 THEN
DEBUG CLS
DEBUG "moving into light"
FOR stride = I TO 20
PWM motor_L, 255, 180
PWM motor_R, 255, 255
NEXT
LOW Motor_L
LOW Motor_R
DEBUG CLS
DEBUG "charging"
DO UNTIL co_ambi > 1
HIGH rc_ambi
PAUSE 100
RCTIME rc_ambi, 1, co_ambi
LOOP
ELSE
ENDIF
RETURN

direction:
HIGH rc_L
PAUSE 100
RCTIME rc_L, 1, cycle_L
HIGH rc_R
PAUSE 100
RCTIME rc_R, 1, cycle_R
DEBUG CLS
DEBUG ? co_ambi
DEBUG ? cycle_L
DEBUG ? cycle_R
RETURN

drive:
SELECT cycle_L
CASE < cycle_R
PWM motor_R, 255, 255
CASE > cycle_R
PWM motor_L, 255, 180
CASE = cycle_R
FOR stride = I TO 4
PWM motor_L, 255, 180
PWM motor_R, 255, 255
NEXT
ENDSELECT
LOW Motor_L
LOW Motor_R
RETURN

avoid:
HIGH trigger
PULSOUT trigger, 3
PULSIN recieve, 1, distance
SELECT distance
CASE < 800
IF cycle_L < cycle_R THEN
HIGH motor_R
LOW Motor_L
PAUSE 10000
ELSE
HIGH motor_L
LOW Motor_R
PAUSE 4000
ENDIF
ENDSELECT
LOW Motor_L
LOW Motor_R
DEBUG ? distance
RETURN




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

cs PIN 0 'chip select
clk PIN 1 'clock
setup PIN 2 'ADC setup
serial_D PIN 3 'serial data transfer pin
red PIN 12 'red LED
grn PIN 13 'grean LED
yel PIN 14 'yellow LED
shut_down PIN 11 'run pin

therm VAR Word 'variable for temperature data
ambient VAR Word 'variable for ambient temp data
pot VAR Word 'potentiometer counts 0-5 volts = 0-255 counts
I VAR Byte 'variable for counter
colors VAR OUTD 'variable output for LEDs

DIRA = %0111 'I/O declarations
DIRD = %0111

GOSUB flash_LEDs 'flashes 3 leds in a binary sequence red yel grn
GOSUB gather_ambient 'checks ambient temperature
DO WHILE shut_down = 1 'program will run untill = 0
GOSUB gather_DATA 'gathers serial byte of temperature data
GOSUB display 'displays temperature data
LOOP

DIRD = %0000

END

flash_LEDs: 'counts to 7 in binary with LED's
FOR colors = I TO 7
PAUSE 500
NEXT
PAUSE 1000
RETURN

gather_ambient: 'records ambient temperature
clk = 0
cs = 1
cs = 0
setup = 1 'specifies single ended mode
PAUSE 5
PULSOUT clk, 25
PAUSE 5
PULSOUT clk, 25
setup = 1 'tells ADC to use channel 0
PAUSE 5
PULSOUT clk, 25
PAUSE 5
SHIFTIN serial_d, clk, MSBPOST, [ambient\8]
ambient=ambient*2
DEBUG DEC ambient, " ambient temp in degrees C resolved to tenths"
DEBUG CR
RETURN

gather_data: 'gathers current temperature
clk = 0
cs = 1
cs = 0
setup = 1 'specifies single ended mode
PAUSE 5
PULSOUT clk, 25
PAUSE 5
PULSOUT clk, 25
setup = 0 'tells ADC to use channel 1
PAUSE 5
PULSOUT clk, 25
PAUSE 5
SHIFTIN serial_d, clk, MSBPOST, [pot\8]
pot = pot*2
DEBUG DEC pot, " pot counts in 'C' resolved to 1/10th of a degree"
DEBUG CR
clk = 0
cs = 1
cs = 0
setup = 1 'specifies single ended mode
PAUSE 5
PULSOUT clk, 25
PAUSE 5
PULSOUT clk, 25
setup = 1 'tells ADC to use channel 0
PAUSE 5
PULSOUT clk, 25
PAUSE 5
SHIFTIN serial_d, clk, MSBPOST, [therm\8]
cs = 1
therm=therm*2
DEBUG DEC therm, " temperature in degrees C resolved to tenths"
DEBUG CR
RETURN

display: 'select case led output, compares ambient to current
SELECT pot
CASE > therm
grn = 1
CASE < grn =" 0"> (ambient+50)
red = 1
DEBUG "Five degrees Above Ambient"
DEBUG CR
PAUSE 5000
CASE < (ambient-50) yel = 1 DEBUG "five degrees Below Ambient" DEBUG CR PAUSE 5000 CASE ELSE yel = 1 red = 1 PAUSE 250 yel = 0 red = 0 PAUSE 5000 ENDSELECT
RETURN








0 Comments:

Post a Comment

<< Home