Skip to content

Instantly share code, notes, and snippets.

@bricklife
Last active July 4, 2020 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricklife/2a66e060ff82add96df23a84a4b7f434 to your computer and use it in GitHub Desktop.
Save bricklife/2a66e060ff82add96df23a84a4b7f434 to your computer and use it in GitHub Desktop.
Autonomous running train in Pybricks
from pybricks.hubs import CityHub
from pybricks.pupdevices import DCMotor, ColorDistanceSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait
hub = CityHub()
motor = DCMotor(Port.A)
color_sensor = ColorDistanceSensor(Port.B)
def wait_for_color(color):
count = 0
while count < 10:
if color_sensor.color() == color:
count += 1
else:
count = 0
wait(10)
while True:
motor.dc(20)
hub.light.on(Color.YELLOW)
wait_for_color(Color.YELLOW)
motor.stop()
wait(1000)
motor.dc(-20)
hub.light.on(Color.BLUE)
wait_for_color(Color.BLUE)
motor.stop()
wait(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment