Last active
July 4, 2020 22:32
-
-
Save bricklife/2a66e060ff82add96df23a84a4b7f434 to your computer and use it in GitHub Desktop.
Autonomous running train in Pybricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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