import machine
import time
# Define pin numbers for ultrasonic sensors
TRIGGER1_PIN = 23
ECHO1_PIN = 19
TRIGGER2_PIN = 22
ECHO2_PIN = 21
# Define pin numbers for servo motors
SERVO1_PIN = 12
SERVO2_PIN = 14
# Define pin numbers for IR sensors
IR1_PIN = 18
IR2_PIN = 05
# Define pin numbers for LED indicators
LED1_PIN = 04
LED2_PIN = 02
THRESHOLD_DISTANCE=100
# Initialize ultrasonic sensors
trigger1 = machine.Pin(TRIGGER1_PIN, machine.Pin.OUT)
echo1 = machine.Pin(ECHO1_PIN, machine.Pin.IN)
trigger2 = machine.Pin(TRIGGER2_PIN, machine.Pin.OUT)
echo2 = machine.Pin(ECHO2_PIN, machine.Pin.IN)
# Initialize servo motors
servo1 = machine.PWM(machine.Pin(SERVO1_PIN), freq=50)
servo2 = machine.PWM(machine.Pin(SERVO2_PIN), freq=50)
# Initialize IR sensors
ir1 = machine.Pin(IR1_PIN, machine.Pin.IN)
ir2 = machine.Pin(IR2_PIN, machine.Pin.IN)
# Initialize LED indicators
led1 = machine.Pin(LED1_PIN, machine.Pin.OUT)
led2 = machine.Pin(LED2_PIN, machine.Pin.OUT)
# Function to measure distance using ultrasonic sensor
def measure_distance(trigger_pin, echo_pin):
trigger_pin.value(0)
time.sleep_us(2)
trigger_pin.value(1)
time.sleep_us(10)
trigger_pin.value(0)
while echo_pin.value() == 0:
pulse_start = time.ticks_us()
while echo_pin.value() == 1:
pulse_end = time.ticks_us()
pulse_duration = pulse_end - pulse_start
distance = (pulse_duration * 34300) / 2000000 # Speed of sound is 343 m/s
return distance
# Function to control servo motor for gate
def move_servo(servo, angle):
duty = (angle / 180) * 1024 + 512
servo.duty(int(duty))
# Main loop
while True:
# Measure distances
distance1 = measure_distance(trigger1, echo1)
distance2 = measure_distance(trigger2, echo2)
# Check if parking spot 1 is occupied
if ir1.value() == 0:
led1.on()
else:
led1.off()
# Check if parking spot 2 is occupied
if ir2.value() == 0:
led2.on()
else:
led2.off()
# Control servo motors based on distance
if distance1 < THRESHOLD_DISTANCE:
move_servo(servo1, 90) # Open gate for parking spot 1
else:
move_servo(servo1, 0) # Close gate for parking spot 1
if distance2 < THRESHOLD_DISTANCE:
move_servo(servo2, 90) # Open gate for parking spot 2
else:
move_servo(servo2, 0) # Close gate for parking spot 2
time.sleep(0.1) # Adjust delay as needed