from machine import Pin, PWM
import time
# 1. تعريف الأطراف
PIR_PIN = 19
BUZZER_PIN = 18
LED_RED_PIN = 5
LED_GREEN_PIN = 4
SEGMENT_PINS = [21, 16, 32, 0, 2, 34, 35]
# 2. تهيئة المخرجات
pir = Pin(PIR_PIN, Pin.IN)
led_red = Pin(LED_RED_PIN, Pin.OUT)
led_green = Pin(LED_GREEN_PIN, Pin.OUT)
# تهيئة الـ Buzzer باستخدام PWM للتحكم في الصوت
buzzer = PWM(Pin(BUZZER_PIN))
buzzer.freq(1000) # تردد الصوت (1kHz)
buzzer.duty(0) # متوقف تماماً في البداية
segments = [Pin(pin, Pin.OUT) for pin in SEGMENT_PINS]
# 3. أنماط الـ 7-Segment (S=Safe, A=Alert)
PAT_S = [1, 0, 1, 1, 0, 1, 1]
PAT_A = [1, 1, 1, 0, 1, 1, 1]
motion_detected = False
def display_character(pattern):
for i in range(7):
segments[i].value(pattern[i])
def handle_interrupt(pin):
global motion_detected
motion_detected = True
# ربط المقاطعة بالحساس
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
# 4. الحلقة الرئيسية
while True:
if motion_detected:
# حالة الإنذار (Alert State)
display_character(PAT_A)
led_red.value(1)
led_green.value(0)
buzzer.duty(512) # تشغيل صوت الـ Buzzer
time.sleep(2) # إبقاء الإنذار لمدة ثانيتين
# إذا توقفت الحركة، ننهي حالة الإنذار
if pir.value() == 0:
motion_detected = False
else:
# حالة الأمان (Safe State)
display_character(PAT_S)
led_red.value(0)
led_green.value(1)
buzzer.duty(0) # إيقاف صوت الـ Buzzer تماماً
time.sleep(0.1)