print("\nMiniproject")
print("Created By : MUHAMMAD ASYRAAF BIN ASRI")
print("Date: 24/5/2024\n")
#Import libraries/module
from machine import Pin, PWM
from utime import sleep
import servo1_library, servo2_library, ultrasonic1_library, ultrasonic2_library
#Pin declaration
Red_led1 = Pin(25,Pin.OUT)
Green_led1 = Pin(26,Pin.OUT)
Red_led2 = Pin(4,Pin.OUT)
Green_led2 = Pin(2,Pin.OUT)
trig1 = Pin(14)
echo1 = Pin(27)
trig2 = Pin(5)
echo2 = Pin(18)
servo_pin1 = Pin(32,Pin.OUT)
servo_pin2 = Pin(33,Pin.OUT)
PIR_pin1 = Pin(13,Pin.IN)
PIR_pin2 = Pin(12,Pin.IN)
#Parameter declaration
#Create an OBJECT name for module with library
sensor1 = ultrasonic1_library.HCSR04(trigger_pin=trig1, echo_pin=echo1)
sensor2 = ultrasonic2_library.HCSR04(trigger_pin=trig2, echo_pin=echo2)
entrance_gate = servo1_library.Servo(pin=servo_pin1)
exit_gate = servo2_library.Servo(pin=servo_pin2)
#main program
while True:
distance_in_cm1 = sensor1.distance_cm()
distance_in_cm2 = sensor2.distance_cm()
entrance_motion_status = PIR_pin1.value() # .value() to read digital value (1 or 0) for entrance
exit_motion_status = PIR_pin2.value() # .value() to read digital value (1 or 0) for exit
entrance_gate.move(angle=0) #initialize
exit_gate.move(angle=0) #initialize
print("The entrance motion status is", entrance_motion_status)
print("The exit motion status is", exit_motion_status)
print("The distance for parking spot 1 is", distance_in_cm1, "cm")
print("The distance for parking spot 2 is", distance_in_cm2, "cm")
# Entrance gate control
if entrance_motion_status == True: # PIR sensor logic for entrance
entrance_gate.move(angle=90) # Open the entrance gate
sleep(3) # Wait for 3 seconds
entrance_gate.move(angle=0) # Close the entrance gate
# Exit gate control
if exit_motion_status == True: # PIR sensor logic for exit
exit_gate.move(angle=90) # Open the exit gate
sleep(3) # Wait for 3 seconds
exit_gate.move(angle=0) # Close the exit gate
# Parking spot status based on distance for parking spot 1
if distance_in_cm1 > 100:
Green_led1.on()
Red_led1.off()
else:
Green_led1.off()
Red_led1.on()
# Parking spot status based on distance for parking spot 2
if distance_in_cm2 > 100:
Green_led2.on()
Red_led2.off()
else:
Green_led2.off()
Red_led2.on()
sleep(1.5) # Delay to prevent rapid state changes