from machine import Pin
from hcsr04 import HCSR04
import time
# Define the pins for HC-SR04
trigger_pin = 18
echo_pin = 5
# Define the LED pins
led_pins = [Pin(pin, Pin.OUT) for pin in [12, 14, 27, 26, 25]]
# Initialize the HC-SR04 sensor
sensor = HCSR04(trigger_pin, echo_pin)
# Function to update the LED bar graph
def update_led_bar(distance):
# Determine the number of LEDs to light up
leds_to_light = min(int(distance / 6), n)
# Update LEDs
for i in range(n):
if i < leds_to_light:
np[i] = (255, 0, 0) # Set the LED to white (or any color you prefer)
else:
np[i] = (0, 255, 0) # Turn off the LED
np.write()
# Main loop
while True:
distance = sensor.distance_cm()
update_led_bar(distance)
time.sleep(0.1)