from machine import Pin,I2C
import time
from hcsr04 import HCSR04
from time import sleep
from i2c_lcd import I2cLcd
import ssd1306
led_pin = 20
led = Pin(led_pin, Pin.OUT)
button_pin = 16
button = Pin(button_pin, Pin.IN, Pin.PULL_UP) # Assuming a pull-up resistor
sensor = HCSR04(trigger_pin=4, echo_pin=5, echo_timeout_us=10000)
# Initial LED state
led_state = False
led.value(led_state)
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(47), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
lcd.clear()
i2c_2 = I2C(0, scl=Pin(48), sda=Pin(45))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_2)
try:
while True:
lcd.move_to(0, 0)
lcd.putstr("2x16 LCD demo")
lcd.move_to(0, 1)
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
sleep(1)
lcd.putstr("Distance: %f" % (distance))
print("Distance: %f" % (distance))
sleep(1)
# Check if the button is pressed (button_pin will be LOW if pressed)
if not button.value():
# Debounce delay
time.sleep(0.05)
if not button.value():
# Toggle the LED state
led_state = not led_state
led.value(led_state)
oled.fill(0)
oled.show()
oled.text('Hello, Wokwi!', 10, 10)
oled.text('LED: ', 10, 20)
oled.text(str(led_state), 42, 20)
oled.show()
# Wait until the button is released
while not button.value():
time.sleep(0.05)
time.sleep(0.05) # Polling interval
except KeyboardInterrupt:
print("Script stopped")