from machine import Pin, ADC
import time
try:
from ulab import numpy as np
except ImportError:
class DummyNP:
def array(self, data): return data
def dot(self, a, b):
return sum(i * j for i, j in zip(a, b))
np = DummyNP()
def light_control(sensor_input):
W = np.array([1, 1])
B = -1.5
A = np.dot(sensor_input, W) + B
return 1 if A > 0 else 0
pir = Pin(13, Pin.IN)
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
led = Pin(4, Pin.OUT)
print("--- AI Light Control System Ready ---")
print("เงื่อนไข: ไฟจะติดเมื่อ (มีคน=1 AND มืด=1)")
while True:
has_person = pir.value()
current_light = ldr.read()
is_dark = 1 if current_light > 2000 else 0
X_input = np.array([has_person, is_dark])
status = light_control(X_input)
led.value(status)
print(f"LDR Raw: {current_light:4} | Matrix: {X_input} -> ไฟ: {'[เปิด]' if status else '[ปิด]'}")
time.sleep(0.5)