import time
import board
import digitalio
# Define ultrasonic sensor pins
trigger_pins = [board.GP14, board.GP16, board.GP18]
echo_pins = [board.GP15, board.GP17, board.GP19]
# Define pulse output pins for the sensors
led_pins = [board.GP20, board.GP21, board.GP22]
leds = [digitalio.DigitalInOut(pin) for pin in led_pins]
for led in leds:
led.direction = digitalio.Direction.OUTPUT
# Define ultrasonic sensor objects
sensors = []
for trig, echo in zip(trigger_pins, echo_pins):
trigger = digitalio.DigitalInOut(trig)
trigger.direction = digitalio.Direction.OUTPUT
echo = digitalio.DigitalInOut(echo)
echo.direction = digitalio.Direction.INPUT
sensors.append((trigger, echo))
# Loop to read distances from the sensors
d=[]
val=0
while True:
for i, (trigger, echo) in enumerate(sensors):
# Send a 10us trigger signal
trigger.value = False
time.sleep(0.0001)
trigger.value = True
time.sleep(0.00001)
trigger.value = False
# Measure the duration of the echo pulse
pulse_start = 0
while echo.value == False:
pulse_start = time.monotonic_ns()
pulse_end = 0
while echo.value == True:
pulse_end = time.monotonic_ns()
pulse_duration = pulse_end - pulse_start
# Calculate the distance in cm
distance = pulse_duration / 58000
# Print the distance and adjust LED brightness based on distance
print("Sensor {} distance: {}".format(i+1, distance))
leds[i].value = distance <= 20
d.append(distance)
if(d[2] <= 50):
val=3
elif(d[1] <= 50):
val=2
elif(d[0] <= 50):
val=1
time.sleep(5)
print("value to be uploaded to thingspeak is",val)
val=0
d.clear()