Complete code for the line-following buggy. You’ll need a suitable course for the buggy which you can create using a large sheet of white paper and
black tape or marker pen.
# buggy-line.py
# Buggy line following code
# Stop motors
pin8.write_digital(1)
pin12.write_digital(1)
pin0.write_digital(1)
pin16.write_digital(1)
return
sleep(coast_time)
return
# Reverse motors
pin8.write_digital(1)
pin12.write_digital(0)
pin0.write_digital(1)
pin16.write_digital(0)
sleep(run_time)
# Coast motors
buggy_coast()
return
if direction == 'right':
# Show right arrow
display.show(Image.ARROW_NE)
sleep(time)
# Coast motors
buggy_coast()
return
# Spins the buggy in a set direction for a number of seconds
# direction (string): one of 'right' or 'left'
# time (number): time in secondsback
def buggy_spin(direction, time):
if direction == 'right':
# Show right arrow
display.show(Image.ARROW_E)
sleep(time)
# Coast motors
buggy_coast()
return
return
if p1 == p2:
# Straight on
buggy_move('forward', run_time)
elif p1 == 1 and p2 == 0:
# Left dark, go left
buggy_spin('left', turn_time)
buggy_turn('left', turn_time)
elif p1 == 0 and p2 == 1:
# Right dark, go right
buggy_spin('right', turn_time)
buggy_turn('right', turn_time)
# The End