Skip to content

Instantly share code, notes, and snippets.

@nbogie
Last active September 28, 2017 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbogie/664dd8e048198842a84d6d3786ad0d2d to your computer and use it in GitHub Desktop.
Save nbogie/664dd8e048198842a84d6d3786ad0d2d to your computer and use it in GitHub Desktop.
us-100 ultrasonic theremin on microbit. Uses fizban99's code to get distance.
from microbit import uart, pin14, pin15, pin2, pin1, pin0, sleep, display
from sys import print_exception
import music
#This is a simplified version of the library here:
# https://github.com/fizban99/microbit_us100
#
# TODO: Perhaps see the above for exception handling.
class US100:
def __init__(self, tpin=pin15, rpin=pin14):
self.tx_pin = tpin
self.rx_pin = rpin
def distance_mm(self):
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=self.tx_pin, rx=self.rx_pin)
sleep(1)
uart.write(b'\x55')
t = 0
buf = bytearray(2)
while not uart.any() and t < 1000:
t = t + 1
sleep(5)
if t < 1000:
uart.readinto(buf, 2)
uart.init(115200)
dist = buf[0] * 256 + buf[1]
if dist > 1100:
dist = -1
return dist
sonar=US100(pin1, pin2)
while True:
d = sonar.distance_mm()
music.pitch(d, -1, pin0, False)
sleep(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment