from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
from hcsr04 import HCSR04
sensor=HCSR04(trigger_pin=5,echo_pin=18,echo_timeout_us=100000)
i2c=SoftI2C(scl=Pin(22),sda=Pin(21),freq=100000)
I2C_ADDR1=0x3c
I2C_ADDR=0x27
oled_width=128
oled_height=64
totalRows=2
totalColumns=16
oled=SSD1306_I2C(oled_width,oled_height,i2c,I2C_ADDR1)
oled.invert(False)
oled.pixel(21,24,1)
lcd=I2cLcd(i2c,I2C_ADDR,totalRows,totalColumns)
while True:
distance=sensor.distance_cm()
print('Distance:',distance,'cm')
oled.fill(0)
oled.text('Distance:',0,0,1)
oled.text(str(distance)+' cm',0,10,1)
oled.show()
lcd.clear()
lcd.putstr('Distance: '+str(distance)+' cm')
sleep(2)