do
readadc 1,b0
sertxd (#b0,13,10)
loop
b1=155+2 ‘ maximum ADC reading+2 (left side)
b2=143 ‘ left slow forward
b4=134 ‘ right slow forward
servo 2,b2 ‘ start left servo
servo 4,b4 ‘ start right servo
Figure 13.
do ‘ start loop
readadc 1,b0 ‘ 156left 116right
b5=b1-b0/3 ‘ sensitivity
b6=b2-b5+7 ‘ left
b7=b4-b5+7 ‘ right
servopos 2,b6 ‘ left servo
servopos 4,b7 ‘ right servo
loop ‘ end loop
photocells need to receive only light reflected from the
ground.
Adjust overall speed with b2 and b4 values, and adjust
the line centering with your b1 value. It’s a balancing act —
certainly part science and part art — to tweak all the
settings for best and fastest performance. The equations for
b5, b6, and b7 calculate new SERVOPOS values based on
the ADC readings, assuming a range of 40 between
extremes. You can adjust the sensitivity (response speed) by
changing the equations:
Select a bright diffused (wide angle) LED in green,
yellow, or white. These old-school cadmium sulfide
photocells are most sensitive to yellow-green light. Your
resistor value will depend on your LED; use no more than
Higher: b5=b1-b0/2 : b6=b2-b5+10 : b7=b4-b5+10
Lower: b5=b1-b0/4 : b6=b2-b5+5 : b7=b4-b5+5
20 mA current. The PCB acts as an overhead light shield,
and the whole assembly is mounted very low — about
0.100” above the ground. I used some scrap styrene and
glued it to the removable battery door. I can swap battery
doors to remove it since the servo connector easily unplugs
from the breadboard.
The graph in Figure 13 shows the characteristic sensor
output. The voltage is nearly constant when both photocells
receive balanced light levels, but at either edge of the line
the photocells are unbalanced and the output voltage is
driven up (left) or down (right). It’s very helpful to use the
onboard DVM to monitor the actual sensor voltages going
into the ADC. Draw a 7. 5” diameter circle on a piece of
blank white paper with a wide black magic marker (line
width ~3/8”). Run this program and manually move your
sensor across the line; you’ll see your ADC values onscreen
in Serial Terminal:
Finally, let’s add a distance sensor to Cruiser. In Part 3,
we learned to use a Sharp IR module which outputs an
analog signal suitable for reading with an ADC. We can
read one on pin C.1 with READADC just like our
homemade line sensor, which we’ll temporarily unplug. I
mounted my sensor using styrene, a paperclip, and some
super glue to the front of Cruiser so I can rotate it forward
or up as shown in Figure 14.
Here’s a program that lets you virtually “push and pull”
Cruiser. It uses proportional speeds and tries to maintain an
~ 8” distance from your hand, where the ADC value is
coincidentally ~80. Cruiser stops if nothing is within 24”
(see the video at www.youtube.com/watch?v
=aMdjEUOm8kI):
I measured 155 max on the left side of the line; 116
max on the right side of line. Yours should be close to
those values. Adjust sensor height AGL and LED resistor
values to get a range of 40. Now, try this simple program
where Cruiser forges steadily forward as pin C.1 reads the
sensor voltage with its ADC and nudges the servos to speed
up or slow down. You need to input b1, your maximum
SERVO 2,139 ‘left stop point
SERVO 4,138 ‘right stop point
DO ‘ start loop
b2=139 ‘ left stop point
b4=138 ‘ right stop point
READADC 1,B0 ‘ read sensor
‘60<B0<120 range setpoint 80
pause 20 ‘ pause 20 ms
if b0<40 then move ‘ nothing nearby
b1=b0/4 ‘ scale reading for servo
b3=20 ‘ also scale setpoint 80/4
b2=b2+b3-b1 ‘ calculate left value
b4=b4+b1-b3 ‘ calculate right value
move:
servopos 2,b2 ‘ drive left servo
servopos 4,b4 ‘ drive right servo
loop
ADC reading plus 2, and SERVOPOS values b2 and b4 for
a slow straight forward creep; try ± 3-4 from your servo’s
stop/null values:
Finally, let’s make Cruiser gesture controlled. By
rotating the sensor facing up, we can hold our hand above
it at different heights to drive it around in different
directions using the same forward/back/left/right values
calibrated previously (watch the video at
42 SERVO 01.2016