import network
from machine import I2C, Pin
from i2c_lcd import I2cLcd
from time import sleep
from hcsr04 import HCSR04
# Pin definitions
TRIGGER_PIN = 23
ECHO_PIN = 19
BUTTON_PIN = 5
BUTTON_N_PIN = 18
LED_PIN = 4
LED_N_PIN = 2
LED_NN_PIN = 15
LCD_SCL_PIN = 22
LCD_SDA_PIN = 21
LCD_ADDRESS = 0x27
# Initialize hardware objects
sensor = HCSR04(trigger_pin=TRIGGER_PIN, echo_pin=ECHO_PIN)
led = Pin(LED_PIN, Pin.OUT)
led_n = Pin(LED_N_PIN, Pin.OUT)
led_nn = Pin(LED_NN_PIN, Pin.OUT)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
button_n = Pin(BUTTON_N_PIN, Pin.IN, Pin.PULL_UP)
i2c = I2C(scl=Pin(LCD_SCL_PIN), sda=Pin(LCD_SDA_PIN), freq=400000)
lcd = I2cLcd(i2c, LCD_ADDRESS, 2, 16)
# Initialize LCD screen
lcd.move_to(3, 0)
lcd.putstr('Micropython')
sleep(2)
lcd.move_to(3, 0)
lcd.putstr('cam on ban ')
lcd.move_to(0, 1)
lcd.putstr("hello fschooler")
# Main loop
while True:
distance = sensor.distance_cm()
print(distance)
sleep(0.1)
# Control LED_NN based on distance
if distance <= 100:
led_nn.value(1)
else:
led_nn.value(0)
# Control LED based on button
button_status = button.value()
if button_status == 0 and button_status != prev_button_status:
led.value(not led.value())
prev_button_status = button_status
# Control LED_N based on button_n
button_n_status = button_n.value()
if button_n_status == 0 and button_n_status != prev_button_n_status:
led_n.value(not led_n.value())
prev_button_n_status = button_n_status
sleep(0.1)