from machine import Pin, I2C
from machine import sleep
import ssd1306
from hcsr04 import HCSR04
from time import sleep
BUTT1 = Pin(12, Pin.IN)
BUTT2 = Pin(4, Pin.IN)

# ESP32 Pin assignment 
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

sensor=HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)

while True:

    distance = str(sensor.distance_cm())
    print('Distance=',distance,'cm')
    sleep(0.0005)


    if BUTT1.value() == 1:
        
        oled.text('RED', 60, 20)      
        oled.show()
    
    
    if BUTT1.value() == 0:
        oled.fill(0)
        oled.text('Distance=',0,10)
        oled.text(distance,70,10)       
        oled.show()
    
    if BUTT2.value() == 0:
        oled.fill(0)
        oled.text('Distance=',0,10) 
        oled.text(distance,70,10)      
        oled.show()
        

   
    if BUTT2.value() == 1:
        
        oled.text('GREEN', 20, 30)      
        oled.show()


Loading
ssd1306