from machine import Pin, I2C, PWM
from hcsr04 import HCSR04
import ssd1306
import time
buzz = PWM(Pin(27))
i2c = I2C(0, scl=Pin(32), sda=Pin(33))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C (oled_width, oled_height, i2c)
hcsr04 = HCSR04(trigger_pin=18, echo_pin=5, echo_timeout_us=100000)
def sound_buzz(frequency, duration_ms):
buzz.freq(frequency)
buzz.duty(512)
time.sleep(duration_ms)
buzz.duty()
while True:
distance = hcsr04.distance_cm()
print(distance)
oled.fill(0)
oled.text("distance", 5,10)
oled.text('{:.2f}'.format(distance) , 10, 20)
oled.show
if distance < 100:
sound_buzz (700, 70)
else:
buzz.duty(0)
time.sleep(0.1)