official skill

Avoid obstacle

avoid_obstacle.py

Cruise until something is close, then stop and beep.

drivesafety
Author
MyRoboBrick
Build
MRB Build v0.1
Language
MicroPython · SPIKE Prime
avoid_obstacle.py
1from spike import PrimeHub, Motor, DistanceSensor2 3hub = PrimeHub()4left = Motor("C")5right = Motor("D")6eye = DistanceSensor("B")7 8SAFE_CM = 129 10def cruise(speed=40):11    left.start(speed)12    right.start(speed)13    while True:14        distance = eye.get_distance_cm()15        # get_distance_cm() returns None when nothing is in range.16        if distance is not None and distance < SAFE_CM:17            left.stop()18            right.stop()19            hub.speaker.beep(70, 0.2)20            return21 22cruise()

Read every line before running it on real hardware.

Have a skill worth sharing?

Submit your own MicroPython skill. Everything is reviewed for safety before it shows up for other builders.