Skip to content

Instantly share code, notes, and snippets.

@cedricbonhomme
Last active December 9, 2023 10:37
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 cedricbonhomme/0032f5418e6efbf63dda325586665983 to your computer and use it in GitHub Desktop.
Save cedricbonhomme/0032f5418e6efbf63dda325586665983 to your computer and use it in GitHub Desktop.
ev3dev with Python and the Mindstorm robot.
#! /usr/bin/env python3
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.motor import OUTPUT_B, OUTPUT_C, SpeedPercent, MoveTank
from ev3dev2.sensor.lego import InfraredSensor, TouchSensor
sound = Sound()
leds = Leds()
ts = TouchSensor()
ir = InfraredSensor()
tank_drive = MoveTank(OUTPUT_B, OUTPUT_C)
leds.set_color("LEFT", "GREEN")
leds.set_color("RIGHT", "GREEN")
sound.speak("Hello Elliot ! Attrape moi !")
sound.speak("Trois, deux, un. Go !")
while not ts.wait_for_pressed(1):
if ir.proximity < 55:
# 22% should be approximately 16cm: obstacle detected !
# change color of the leds then turn...
leds.set_color("LEFT", "RED")
leds.set_color("RIGHT", "RED")
tank_drive.on_for_seconds(SpeedPercent(60), SpeedPercent(30), 4)
else:
leds.set_color("LEFT", "GREEN")
leds.set_color("RIGHT", "GREEN")
# drive in a turn for 2 rotations of the outer motor
# the first two parameters can be unit classes or percentages.
tank_drive.on_for_rotations(SpeedPercent(90), SpeedPercent(90), 3)
sound.speak("Bye-bye !")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment