How to move my ev3 robot a certain distance and get it to stop?

683 Views Asked by At

I'm starting out with an EV3 lego robot and my first project to pop the cherry was getting it to move a certain distance, and having it stop when the ultrasonic sensor dectects something 3 cm away. But I have tried so many things and can't get it to work right. here is my code:

#!/usr/bin/env python3
from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C, seconds
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor.lego import UltrasonicSensor
from time import sleep
from time import seconds


us = UltrasonicSensor()
tank_pair = MoveTank(OUTPUT_B, OUTPUT_C, seconds)


tank_pair.on(left_speed=30, right_speed=30)

if distance_centimeters(10):  
    tank_pair.off()

Any help would be appreciated.

1

There are 1 best solutions below

0
Tim Roberts On

I believe you need something like this:

import time
...
while us.distance_centimeters() > 10.0:
    time.sleep( 0.1 )
  
tank_pair.off()