Originally Posted by
scaenix
'Read Cds cell #1 on port b line 1
'Read Cds cell #2 on port b line 7
v1 var byte 'Variable v1 holds Cds #1 information
v2 var byte 'Variable v2 holds Cds #2 information
v3 var byte 'Variable for calculation
s1 var byte 'Variable s1 holds servomotor #1 pulse width info
s2 VAR word 'variable for random function
rv var byte 'variable rv holds the range value
s1 = 150 'initialize steering servomotor facing forward
rv = 10 'Adjust as needed for operation
ct var byte 'counter
start:
pot portb.2,255,v1
pot portb.3,255,v2
'Check bumper switch "Did I hit something?"
if porta.0 = 0 then avoid 'Hit obstacle go into avoid mode
'Is it sleepy time?
if v1 <= 230 then skp 'Is it dark enough to sleep?
if v2 > 230 then slp 'Yes
'Is it too bright to see?
skp:
if v1 >= 12 then skip2
if v2 < 12 then avoid
'Which way do I go?
skip2:
if v1 = v2 then straight
if v1 > v2 then greater
if v1 < v2 then lesser
straight:
pulsout portb.6, s1
pulsout portb.7, 157
goto start
greater:
v3 = v1 - v2
if v3 > rv then right
goto straight
lesser:
v3 = v2 - v1
if v3 > rv then left
goto straight
right:
s1 = s1 + 1
if s1 > 225 then s1 = 225
pulsout portb.6, s1
pulsout portb.7, 165
goto start
left:
s1 = s1 - 1
if s1 < 65 then s1 = 65
pulsout portb.6, s1
pulsout portb.7, 165
goto start
slp:
pulsout portb.6, s1
pulsout portb.7, 167
goto start
avoid:
random s2
s1 = s2 / 256
if s1 < 65 then s1 = 65
if s1 > 225 then s1 = 225
for ct = 1 to 125
pulsout portb.6, s1
pulsout portb.7, 177
pause 18
next ct
s1 = 150
goto start