Snek Drives a Lego Car
(click on the picture to see the movie)
This is made from:
You can get everything other than the light sensors from various vendors.
More Pictures
Here's a picture from the front showing the light and light sensor mounted next to each other.
And here's the car from the rear showing the motor connections, the other light sensor/light combination and the battery pack:
Source Code
This program is a bit more verbose:
mr = (MOTOR1A, MOTOR1B)
ml = (MOTOR2A, MOTOR2B)
pf = SIGNAL1
pr = SIGNAL2
f_speed = 0.5
r_speed = 0.5
t_speed_f = 0.6
t_speed_s = 0.2
def forw():
talkto(mr)
setpower(f_speed)
setright()
on()
talkto(ml)
setpower(f_speed)
setleft()
on()
def back():
talkto(mr)
setpower(r_speed)
setleft()
on()
talkto(ml)
setpower(r_speed)
setright()
on()
def left():
talkto(mr)
setpower(t_speed_f)
setright()
on()
talkto(ml)
setpower(t_speed_f)
setright()
on()
def right():
talkto(mr)
setpower(t_speed_f)
setleft()
on()
talkto(ml)
setpower(t_speed_f)
setleft()
on()
def stop():
talkto(ml)
setpower(0)
off()
talkto(mr)
setpower(0)
off()
def go_forw():
forw()
listento(pf)
while read() < .25:
pass
stop()
def go_back():
back()
listento(pr)
while read() < .25:
pass
stop()
def bumper():
while True:
go_forw()
back()
time.sleep(0.5)
stop()
left()
time.sleep(0.25)
stop()
go_forw()
go_back()
go_forw()
bumper()