# -*- coding: utf-8 -*- """ == Programme de mesure de distance d’un module ultrason à 3 broches == """ from microbit import * from utime import ticks_us, sleep_us sonar = pin2 def getDistance(): sonar.write_digital(1) # Send 10us Ping pulse sleep_us(10) sonar.write_digital(0) # Wait until the pulse starts while sonar.read_digital() == 0: pass start = ticks_us() # define starting time # Wait for Echo pulse to return while sonar.read_digital() == 1: pass end = ticks_us() # define ending time echo = end-start distance = int(0.01715 * echo ) # Calculate cm distance return distance while True: if button_a.was_pressed(): # bouton A pour effectuer une mesure dist = getDistance() print(dist) # retour REPL dans la console de l'éditeur display.scroll('%s cm' % dist) sleep_us (2000)
( Christophe Béasse - Mai 2019 )