# main.py
from machine import Pin, SPI
import utime
# Kullanacağınız pcd8544_fb.py dosyasından PCD8544_FB sınıfını import ediyoruz
from pcd8544_fb import PCD8544_FB
# --- Pin Tanımlamaları ---
PIR_SENSOR_PIN = 28
pir_signal_input = Pin(PIR_SENSOR_PIN, Pin.IN)
# LED Pini
LED_PIN = 15
led = Pin(LED_PIN, Pin.OUT)
# Buzzer Pini
BUZZER_PIN = 14
buzzer = Pin(BUZZER_PIN, Pin.OUT)
# Buton Pini
BUTTON_PIN = 13
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# Nokia 5110 LCD Pinleri (SPI0)
spi = SPI(0, baudrate=2000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19))
dc_pin = Pin(16, Pin.OUT) # Data/Command pini
rst_pin = Pin(20, Pin.OUT) # Reset pini
cs_pin = Pin(17, Pin.OUT) # Chip Select pini
lcd = PCD8544_FB(spi, cs=cs_pin, dc=dc_pin, rst=rst_pin)
# --- 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 (5 saniye)
PIR_CALIBRATION_TIME_MS = 1000
# --- Fonksiyonlar ---
def update_lcd():
"""LCD ekranını mevcut duruma göre günceller."""
lcd.fill(0)
current_time = utime.ticks_ms()
elapsed_time = utime.ticks_diff(current_time, startup_time)
if not pir_ready:
# PIR kalibrasyon durumu
remaining_seconds = (PIR_CALIBRATION_TIME_MS - elapsed_time) // 1000
lcd.text("PIR HAZIR DEGIL.", 0, 0, 1) # Önceki "PIR HAZIR." metni düzeltildi.
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) # Önceki "KAPAT: BUTON" metni düzeltildi.
elif system_armed:
lcd.text("SISTEM AKTIF", 0, 0, 1)
lcd.text("PIR IZLIYOR", 0, 10, 1)
lcd.text("BUTON: KAPAT", 0, 20, 1) # Önceki "KAPAT: BUTON" metni düzeltildi.
else: # Sistem devredışı
lcd.text("DEVRE DISI", 0, 0, 1)
lcd.text("BUTON: AC", 0, 10, 1) # Önceki "AKTIF: BUTON" metni düzeltildi.
if pir_ready:
lcd.text("PIR HAZIR", 0, 20, 1)
lcd.show()
def process_button_event():
""" Buton olayı algılandığında çağrılır """
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()
if system_armed and not alarm_triggered:
led.on()
update_lcd()
def read_pir_sensor():
"""PIR sensöründen doğrudan değeri okur."""
pir_value = pir_signal_input.value()
return pir_value
def check_pir_sensor():
"""PIR sensörünü kontrol eder"""
global alarm_triggered
pir_value = read_pir_sensor()
if pir_value == 1:
alarm_triggered = True
led.on()
buzzer.on()
update_lcd()
return True
return False
# --- Ana Kurulum ---
led.off()
buzzer.off()
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()
# Buton okuma ve zıplama engelleme
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: # Buton basıldı
process_button_event()
# Alarm sistemi ana mantığı
if system_armed and pir_ready and not alarm_triggered:
led.on() # Sistem aktifse ve alarm yoksa LED yanık kalır
check_pir_sensor()
elif not system_armed and not alarm_triggered:
# Sistem aktif değilse ve alarm yoksa (örn: yeni kapatıldı veya devredışı)
led.off()
buzzer.off()
# Not: Alarm tetiklendiğinde (alarm_triggered = True), LED ve Buzzer
# check_pir_sensor() içinde zaten açılır ve process_button_event()
# çağrılana kadar açık kalır.
utime.sleep_ms(50)Loading
cd74hc4067
cd74hc4067