from machine import Pin, PWM
from utime import sleep, sleep_ms
from time import sleep

#Pin declare for LED
green_led = Pin(22, Pin.OUT)
red_led = Pin(23, Pin.OUT)

# initialize the PIR sensor on pin 34, LED on pin 12, and servo motor on pin 21
PIR_sensor =Pin(34, Pin.IN)
bar= PWM(Pin(23,Pin.OUT)) #PWM servo toll bar
bar.freq(50)


 #Move the servo motor to a specific angle
def move_servo(angle):
    # map the angle to the duty cycle for the servo motor
    duty_cycle = int((angle / 180) * 50)
    servo_motor.duty(duty_cycle)

#Get loop/repeat
while True:

 # check if motion is detected by the PIR sensor
    if PIR_sensor.value() == 1:
        print("HAVE CAR!")
        print("YEAYYY...YOU ARE READY TO GO!")
        red_led.on()
        sleep(2)
        red_led.off()
        sleep(1)
        # move the servo motor to 180 degrees
        bar.duty_u16(1638)
        sleep(1)
        # wait for 10 seconds
        sleep(10)
        # move the servo motor back to 0 degrees
        bar.duty_u16(4860)

    elif PIR_sensor.value() == 0:
        green_led.on()
        sleep(2)
        green_led.off()
        sleep(1)
        sleep(1)
        print("NO CAR.")
    # wait for a short time to reduce CPU usage
    sleep_ms(10)