Created
September 24, 2018 04:43
-
-
Save shikarunochi/2eac9fbe4945c4cedfe7082d304426f0 to your computer and use it in GitHub Desktop.
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 m5stack import lcd | |
import utime, time | |
import machine | |
lcd.compileFont('x12y20pxScanLine.c') | |
lcd.font('x12y20pxScanLine.fon') | |
#5番ピン(GPIO5)をトリガーピンに設定 | |
trigger_pin = machine.Pin(5, machine.Pin.OUT) | |
trigger_pin.value(0) | |
#2番ピン(GPIO2)をエコーピンに設定 | |
echo_pin = machine.Pin(2, machine.Pin.IN) | |
PULSE_TIMEOUT = 150000 | |
lcd.clear() | |
lcd.setCursor(0, 0) | |
lcd.setColor(lcd.WHITE) | |
while True: | |
#10uS の間、トリガーピンをONに。 | |
trigger_pin.value(1) | |
time.sleep_us(10) | |
trigger_pin.value(0) | |
#エコーピンが ON になるまでの時間を計測 | |
duration = machine.time_pulse_us(echo_pin, 1, PULSE_TIMEOUT) | |
#時間から距離を計測 | |
distance = (duration / 2) * 0.034 | |
#画面表示 | |
lcd.clear() | |
lcd.setCursor(0, 0) | |
lcd.print('{:.1f} cm'.format(distance)) | |
#1秒待ち | |
utime.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment