Created
June 15, 2020 17:52
-
-
Save robmckinnon/e568fc393e252325231ffd08b3bbb640 to your computer and use it in GitHub Desktop.
distance_sensor_midi.py
This file contains hidden or 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
import time | |
import rtmidi | |
from gpiozero import DistanceSensor # Import the GPIO Zero Library | |
# Define GPIO pins to use on the Pi | |
pinTrigger = 17 | |
pinEcho = 18 | |
midiout = rtmidi.MidiOut() | |
available_ports = midiout.get_ports() | |
if available_ports: | |
midiout.open_port(1) | |
else: | |
midiout.open_virtual_port("My virtual output") | |
with midiout: | |
note_on = [0x90, 60, 112] # channel 1, middle C, velocity 112 | |
note_off = [0x80, 60, 0] | |
midiout.send_message(note_on) | |
time.sleep(0.5) | |
midiout.send_message(note_off) | |
time.sleep(0.1) | |
sensor = DistanceSensor(echo=pinEcho, trigger=pinTrigger) | |
last_distance = 0 | |
try: | |
# Repeat the next indented block forever | |
while True: | |
print("Distance: %.1f cm" % sensor.distance * 1000) | |
time.sleep(0.5) | |
# If you press CTRL+C, cleanup and stop | |
except KeyboardInterrupt: | |
del midiout | |
print("Exiting") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment