from machine import Pin, SPI
import utime
from pcd8544_fb import PCD8544_FB
# --- Pin Tanımlamaları ---
PIR_SENSOR_PIN = 28
pir_signal_input = Pin(PIR_SENSOR_PIN, Pin.IN)
# Doğrudan bağlı LED
LED_PIN = 15
led = Pin(LED_PIN, Pin.OUT)
# Buzzer
BUZZER_PIN = 14
buzzer = Pin(BUZZER_PIN, Pin.OUT)
# Buton
BUTTON_PIN = 13
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# Nokia 5110 LCD (SPI0)
spi = SPI(0, baudrate=2000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19))
dc_pin = Pin(16, Pin.OUT)
rst_pin = Pin(20, Pin.OUT)
cs_pin = Pin(17, Pin.OUT)
lcd = PCD8544_FB(spi, cs=cs_pin, dc=dc_pin, rst=rst_pin)
# --- MUX Tanımlamaları ---
mux_select_pins = [Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT)]
mux_sig_pin = Pin(6, Pin.OUT) # SIG pini -> MUX üzerinden LED kontrolü
# --- Global Değişkenler ---
system_armed = False
alarm_triggered = False
pir_ready = False
startup_time = utime.ticks_ms()
# --- Buton Durum Değişkenleri ---
button_physical_state_previous = button.value()
button_debounced_value = button_physical_state_previous
button_last_change_time = utime.ticks_ms()
DEBOUNCE_DELAY_MS = 50
# PIR Kalibrasyon Süresi
PIR_CALIBRATION_TIME_MS = 5000
# --- Fonksiyonlar ---
def select_mux_channel(channel):
"""MUX kanal seçimi yapar (0-15)"""
for i in range(4):
mux_select_pins[i].value((channel >> i) & 1)
def update_lcd():
lcd.fill(0)
current_time = utime.ticks_ms()
elapsed_time = utime.ticks_diff(current_time, startup_time)
if not pir_ready:
remaining_seconds = (PIR_CALIBRATION_TIME_MS - elapsed_time) // 1000
lcd.text("PIR HAZIR DEGIL.", 0, 0, 1)
lcd.text(f"BEKLE: {max(0, remaining_seconds)}s", 0, 10, 1)
elif alarm_triggered:
lcd.text("*** ALARM ***", 0, 0, 1)
lcd.text("HAREKET VAR!", 0, 10, 1)
lcd.text("BUTON: KAPAT", 0, 20, 1)
elif system_armed:
lcd.text("SISTEM AKTIF", 0, 0, 1)
lcd.text("PIR IZLIYOR", 0, 10, 1)
lcd.text("BUTON: KAPAT", 0, 20, 1)
else:
lcd.text("DEVRE DISI", 0, 0, 1)
lcd.text("BUTON: AC", 0, 10, 1)
if pir_ready:
lcd.text("PIR HAZIR", 0, 20, 1)
lcd.show()
def process_button_event():
global system_armed, alarm_triggered
if not pir_ready:
return
if alarm_triggered:
alarm_triggered = False
system_armed = False
elif system_armed:
system_armed = False
else:
system_armed = True
led.off()
buzzer.off()
mux_sig_pin.value(0) # MUX LED kapat
if system_armed and not alarm_triggered:
led.on()
update_lcd()
def read_pir_sensor():
return pir_signal_input.value()
def check_pir_sensor():
global alarm_triggered
pir_value = read_pir_sensor()
if pir_value == 1:
alarm_triggered = True
led.on()
buzzer.on()
# MUX üzerinden LED yak (C0)
select_mux_channel(0)
mux_sig_pin.value(1)
update_lcd()
return True
return False
# --- Başlangıç Ayarları ---
led.off()
buzzer.off()
mux_sig_pin.value(0)
update_lcd()
# --- Ana Döngü ---
while True:
current_time = utime.ticks_ms()
elapsed_time = utime.ticks_diff(current_time, startup_time)
if not pir_ready and elapsed_time >= PIR_CALIBRATION_TIME_MS:
pir_ready = True
update_lcd()
current_button_physical_state = button.value()
if current_button_physical_state != button_physical_state_previous:
button_last_change_time = current_time
button_physical_state_previous = current_button_physical_state
if utime.ticks_diff(current_time, button_last_change_time) > DEBOUNCE_DELAY_MS:
if current_button_physical_state != button_debounced_value:
button_debounced_value = current_button_physical_state
if button_debounced_value == 0:
process_button_event()
if system_armed and pir_ready and not alarm_triggered:
led.on()
check_pir_sensor()
elif not system_armed and not alarm_triggered:
led.off()
buzzer.off()
mux_sig_pin.value(0)
utime.sleep_ms(50)