import machine
import time
from lcd import lcd_init, lcd_print, lcd_clear # Import necessary functions from lcd.py
# Pin setup for ultrasonic sensor
trigger = machine.Pin(13, machine.Pin.OUT)
echo = machine.Pin(12, machine.Pin.IN)
# Initialize LCD
lcd_init()
# Measure distance using ultrasonic sensor
def measure_distance():
trigger.off()
time.sleep_us(2)
trigger.on()
time.sleep_us(10)
trigger.off()
while echo.value() == 0:
pulse_start = time.ticks_us()
while echo.value() == 1:
pulse_end = time.ticks_us()
duration = pulse_end - pulse_start
distance = (duration * 0.034) / 2 # Speed of sound in cm/us
return distance
# Main code
visitor_count = 0
while True:
distance = measure_distance()
print(f"Distance: {distance} cm")
if distance < 30: # Person detected within 30 cm
visitor_count += 1
time.sleep(1) # Avoid multiple counts for the same person
while measure_distance() < 30: # Wait until the person moves away
time.sleep(0.1)
# Display the visitor count on the LCD using lcd.py
lcd_print("Visitors: " + str(visitor_count))
time.sleep(1)