// from gpiozero import DistanceSensor, LED
import time
# Define GPIO pins for ultrasonic sensor
TRIGGER_PIN = 17 # Change this pin number based on your actual GPIO pin connection
ECHO_PIN = 18 # Change this pin number based on your actual GPIO pin connection
# Define GPIO pins for LEDs
LED_1_PIN = 18 # Change this pin number based on your actual GPIO pin connection
LED_2_PIN = 14 # Change this pin number based on your actual GPIO pin connection
# Create ultrasonic sensor and LED objects
ultrasonic_sensor = DistanceSensor(echo=ECHO_PIN, trigger=TRIGGER_PIN)
led1 = LED(LED_1_PIN)
led2 = LED(LED_2_PIN)
# Function to perform actions based on distance measured by the ultrasonic sensor
def perform_action(distance):
# If an object is closer than 10cm, turn on both LEDs; otherwise, turn them off
if distance is not None and distance < 0.1:
led1.on()
led2.on()
else:
led1.off()
led2.off()
try:
while True:
# Read distance from ultrasonic sensor
distance = ultrasonic_sensor.distance
# Perform actions based on the distance
perform_action(distance)
time.sleep(0.1) # Adjust the delay as needed
except KeyboardInterrupt:
# Clean up GPIO settings upon program termination
led1.off()
led2.off()