from machine import Pin
import time
# 1. تعريف الأزرار (Buttons)
button_in = Pin(18, Pin.IN)
button_out = Pin(19, Pin.IN)
# 2. تعريف اللمبات (LEDs)
green_led = Pin(21, Pin.OUT)
red_led = Pin(22, Pin.OUT)
yellow_led = Pin(23, Pin.OUT)
blue_led = Pin(25, Pin.OUT)
# 3. المتغيرات الأساسية (Variables)
MAX_CAPACITY = 15
car_count = 0
# 4. دالة تحديث الإضاءة المعتمدة في الصورة
def update_status_leds():
global car_count
if car_count < MAX_CAPACITY:
green_led.on()
red_led.off()
else:
green_led.off()
red_led.on()
# تشغيل الدالة لأول مرة لتهيئة اللمبات
update_status_leds()
# المتغيرات لمنع تكرار القراءة (Debounce)
last_enter = 1
last_exit = 1
print(f"Garage Initialized. Current count: {car_count}/{MAX_CAPACITY}")
# 5. الحلقة المستمرة (المكتوبة بالصورة)
while True:
current_enter = button_in.value()
current_exit = button_out.value()
# عند الضغط على زر الدخول
if last_enter == 1 and current_enter == 0:
if car_count < MAX_CAPACITY:
car_count += 1
print(f"Car Entered! Total: {car_count}")
yellow_led.on()
time.sleep(0.5)
yellow_led.off()
update_status_leds()
else:
print("Garage is FULL! Cannot enter.")
# عند الضغط على زر الخروج
if last_exit == 1 and current_exit == 0:
if car_count > 0:
car_count -= 1
print(f"Car Exited! Total: {car_count}")
blue_led.on()
time.sleep(0.5)
blue_led.off()
update_status_leds()
last_enter = current_enter
last_exit = current_exit
time.sleep(0.05)