Files
pybricks-api/examples/pup/sensor_ultrasonic/cleanup.py
T
Laurens Valk ffb2f8aa96 pybricks.pupdevices.UltrasonicSensor: Move cleanup example.
Also generalize the example so that it can be used on all hubs.
2022-01-07 13:38:07 +01:00

26 lines
572 B
Python

from pybricks.parameters import Port
from pybricks.pupdevices import UltrasonicSensor
from pybricks.tools import wait
# Initialize the sensor.
sensor = UltrasonicSensor(Port.A)
# Turn on the upper lights.
sensor.lights.on([50, 50, 0, 0])
def main():
while True:
print("Main program is running.")
wait(500)
# Wrap the main code in try/finally so that the cleanup code always runs
# when the program ends, even if an exception was raised.
try:
main()
finally:
# The cleanup code goes here.
print("Cleaning up.")
sensor.lights.off()