├── PoweredUp Code Blocks for 60337 Express Passenger Train.pdf ├── README.md └── pybricks-code-for-60337-express-passenger-train.py /PoweredUp Code Blocks for 60337 Express Passenger Train.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eggybricks/poweredup-color-sensor/HEAD/PoweredUp Code Blocks for 60337 Express Passenger Train.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code examples for PoweredUp color sensor with trains 2 | 3 | Code examples for how to use a LEGO® color sensor to automate a LEGO® train with (1) Pybricks and (2) PoweredUp code blocks to go back and forth forever. 4 | 5 | LEGO® is a trademark of The LEGO Group of companies, which does not in any way sponsor, authorize, or endorse this content. 6 | -------------------------------------------------------------------------------- /pybricks-code-for-60337-express-passenger-train.py: -------------------------------------------------------------------------------- 1 | from pybricks.pupdevices import ColorDistanceSensor, DCMotor # ColorSensor instead of ColorDistanceSensor if using Mindstorms sensor 2 | from pybricks.parameters import Color, Port 3 | from pybricks.tools import wait 4 | 5 | motor = DCMotor(Port.A) 6 | sensor = ColorDistanceSensor(Port.B) # ColorSensor instead of ColorDistanceSensor if using Mindstorms sensor 7 | 8 | station_stop_time_ms = 2500 9 | eol_stop_time_ms = 5000 10 | forward_speed = 20 11 | check_color_interval_ms = 20 12 | 13 | def check_for_color(color): 14 | while sensor.color() != color: 15 | wait(check_color_interval_ms) 16 | 17 | while True: 18 | print("looking for green") 19 | check_for_color(Color.GREEN) 20 | 21 | print("green detected, at station, stopping and continuing") 22 | motor.brake() 23 | wait(station_stop_time_ms) 24 | motor.dc(forward_speed) 25 | 26 | print("looking for yellow") 27 | check_for_color(Color.YELLOW) 28 | 29 | print("yellow detected, at end of line, stopping and going back to station") 30 | motor.brake() 31 | wait(eol_stop_time_ms) 32 | motor.dc(-forward_speed) 33 | 34 | print("looking for green") 35 | check_for_color(Color.GREEN) 36 | 37 | print("green detected, at station, stopping and continuing") 38 | motor.brake() 39 | wait(station_stop_time_ms) 40 | motor.dc(-forward_speed) 41 | 42 | print("looking for red") 43 | check_for_color(Color.RED) 44 | 45 | print("red detected, at end of line, stopping and going back to station") 46 | motor.brake() 47 | wait(eol_stop_time_ms) 48 | motor.dc(forward_speed) 49 | --------------------------------------------------------------------------------