Erste Schritte mit LEGO
# Show how many colored objects we have already scanned.
brick.display.text(len(color_list))
# Wait for the center button to be pressed or a color to be scanned.
while True:
# Store True if the center button is pressed or False if not.
pressed = Button.CENTER in brick.buttons()
# Store the color measured by the Color Sensor.
color = color_sensor.color()
# If the center button is pressed or a color is detected,
# break out of the loop.
if
pressed
break
if
pressed:
# If the button was pressed, end the loop early.
# We will no longer wait for any remaining objects
# to be scanned and added to the chute.
break
else:
# Otherwise, a color was scanned.
# So we add(append) it to the list.
brick.sound.beep(1000, 100, 100)
color_list.append(color)
# We don't want to register the same color once more if we're
# still looking at the same object. So before we continue, we
# wait until the sensor no longer sees the object.
while
color_sensor.color()
pass
brick.sound.beep(2000, 100, 100)
# Show an arrow pointing to the center button,
# to ask if we are done.
brick.display.image(ImageFile.BACKWARD)
wait(2000)
# Play a sound and show an image to indicate that we are done scanning.
brick.sound.file(SoundFile.READY)
brick.display.image(ImageFile.EV3)
# Now sort the bricks according the list of colors that we stored.
# We do this by going over each color in the list in a loop.
for
in
color
color_list:
# Wait for one second between each sorting action.
wait(1000)
# Run the conveyor belt motor to the right position based on the color.
if
color == Color.BLUE:
brick.sound.file(SoundFile.BLUE)
belt_motor.run_target(500, 10)
elif
color == Color.GREEN:
brick.sound.file(SoundFile.GREEN)
belt_motor.run_target(500, 132)
MINDSTORMS
®
®
or
in
color
POSSIBLE_COLORS:
LEGO, the LEGO logo and MINDSTORMS are trademarks of the LEGO Group.
©2019 The LEGO Group.
Education EV3 MicroPython
in
POSSIBLE_COLORS:
Version 1.0.0
(Fortsetzung von der vorherigen Seite)
(Fortsetzung auf der nächsten Seite)
47