Skip to content

Instantly share code, notes, and snippets.

@goteusz-maszyk
Last active April 25, 2024 15:33
Show Gist options
  • Save goteusz-maszyk/bc7a93a3f682bd171a3ac46c86201c7f to your computer and use it in GitHub Desktop.
Save goteusz-maszyk/bc7a93a3f682bd171a3ac46c86201c7f to your computer and use it in GitHub Desktop.
from machine import Pin, I2C, time_pulse_us
from sh1106 import SH1106_I2C
from math import sin
from time import sleep_us, sleep_ms
i2c = I2C(0)
display = SH1106_I2C(128, 64, i2c)
trigger = Pin(16, Pin.OUT)
echo = Pin(17, Pin.IN)
display.fill(0)
display.flip(None)
def f(x) -> int:
return int(sin(x/10)*31) + 30
def get_distance(trigger, echo):
trigger.on()
sleep_us(10)
trigger.off()
time = time_pulse_us(echo, 1, 20000)
return time/2/29.1
ticks = 0
while True:
distance = max(get_distance(trigger, echo), 0)
sleep_ms(1)
display.fill(0)
hours = int(ticks) // 3600
minutes = (int(ticks) - hours * 3600) // 60
seconds = int(ticks) - hours * 3600 - minutes * 60
display.text(f'{int(ticks)}', 0, 0)
# display.text(f"{hours}:{minutes}:{seconds}", 0, 9)
# display.show()
if distance != 0:
ticks += 30/distance
t = int(ticks)
for x in range(t, t+127):
y = 63-f(x)
prev_y = 63-f(x-1)
if y < 0 or prev_y < 0:
continue
display.line(x-t, y, x-1-t, prev_y, 1)
display.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment