from machine import Pin, ADC, PWM
from time import sleep
joystick_x = ADC(Pin(27)) # X-axis → GP27
button = Pin(22, Pin.IN, Pin.PULL_UP) # Button → GP22
slide_switch = Pin(3, Pin.IN) # Switch → GP3
servo = PWM(Pin(16)) # Servo → GP16
servo.freq(50)
# RGB LED
red_led = Pin(0, Pin.OUT)
green_led = Pin(1, Pin.OUT)
blue_led = Pin(2, Pin.OUT)
buzzer = PWM(Pin(28)) # Buzzer → GP28
# Parking slots
slot1 = 0
slot2 = 0
slot3 = 0
slot4 = 0
slot5 = 0
while True:
# Read inputs
x_val = joystick_x.read_u16()
button_state = button.value()
switch_state = slide_switch.value()
# Gate control
if x_val > 43690:
angle = 180
print("Gate Open")
else:
angle = 90
print("Gate Closed")
# Convert angle to servo PWM
pulse_width = 0.5 + (angle / 180) * 2.0
duty = int((pulse_width / 20) * 65535)
servo.duty_u16(duty)
# ---------------- ENTRY MODE ----------------
if switch_state == 1:
print("Entry Mode")
if button_state == 0:
# Find the first available slot
if slot1 == 0:
slot1 = 1
print("Car Parked in Slot 1")
elif slot2 == 0:
slot2 = 1
print("Car Parked in Slot 2")
elif slot3 == 0:
slot3 = 1
print("Car Parked in Slot 3")
elif slot4 == 0:
slot4 = 1
print("Car Parked in Slot 4")
elif slot5 == 0:
slot5 = 1
print("Car Parked in Slot 5")
else:
# All slots occupied
print("Parking Full")
print("No Parking Slot Available")
# Red LED blink
red_led.value(1)
green_led.value(0)
blue_led.value(0)
# Buzzer ON
buzzer.freq(1000)
buzzer.duty_u16(32768)
sleep(0.5)
# Buzzer OFF
buzzer.duty_u16(0)
# Red LED OFF
red_led.value(0)
sleep(0.5)
sleep(0.5)
# ---------------- EXIT MODE ----------------
else:
print("Exit Mode")
if button_state == 0:
# Find the first occupied slot
if slot1 == 1:
slot1 = 0
print("Car Exited")
print("Slot 1 is now available")
elif slot2 == 1:
slot2 = 0
print("Car Exited")
print("Slot 2 is now available")
elif slot3 == 1:
slot3 = 0
print("Car Exited")
print("Slot 3 is now available")
elif slot4 == 1:
slot4 = 0
print("Car Exited")
print("Slot 4 is now available")
elif slot5 == 1:
slot5 = 0
print("Car Exited")
print("Slot 5 is now available")
else:
print("Parking Empty")
sleep(0.5)
# ---------------- PARKING STATUS ----------------
if slot1 == 1 and slot2 == 1 and slot3 == 1 and slot4 == 1 and slot5 == 1:
# Red → Parking Full
red_led.value(1)
green_led.value(0)
blue_led.value(0)
print("Parking Full")
else:
# Green → Parking Available
red_led.value(0)
green_led.value(1)
blue_led.value(0)
buzzer.duty_u16(0)
sleep(0.1)