from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from hcsr04 import HCSR04
from time import sleep
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
sensor = HCSR04(trigger_pin=Pin(23), echo_pin=Pin(19), echo_timeout_us=10000)
I2C_ADDR = 0x3c
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c, I2C_ADDR)
while True:
distance = sensor.distance_cm()
print("Distance:", distance, "cm")
oled.fill(0) # Clear the display
oled.text("Distance:", 0, 0)
oled.text(str(distance) + " cm", 0, 10)
oled.show()
sleep(1)