Created
July 22, 2017 14:55
-
-
Save jimmo/14c6194578f721477c5c88e5dbbcfcef to your computer and use it in GitHub Desktop.
HC-SR04 for micro:bit MicroPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from microbit import * | |
import gc | |
DISTANCE_CM_PER_BIT = 0.21 | |
DISTANCE_OFFSET = 1.8 | |
TRIG_PIN = pin0 | |
ECHO_PIN = pin1 | |
spi.init(baudrate=50000,bits=8,mode=0,miso=ECHO_PIN) | |
def distance(): | |
gc.disable() | |
TRIG_PIN.write_digital(True) | |
TRIG_PIN.write_digital(False) | |
x = spi.read(200) | |
high_bits = 0 | |
for i in range(len(x)): | |
if x[i] == 0 and high_bits > 0: | |
break | |
elif x[i] == 0xff: | |
high_bits += 8 | |
else: | |
high_bits += bin(x[i]).count('1') | |
x = None | |
gc.enable() | |
gc.collect() | |
return high_bits * DISTANCE_CM_PER_BIT + DISTANCE_OFFSET | |
while True: | |
print(str(distance())) | |
sleep(200) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment