from machine import ADC, Pin
import time
import math
ldr = ADC(26)
led = Pin(15, Pin.OUT)
buzzer = Pin(14, Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
system_on = True
while True:
if button.value() == 1:
system_on = not system_on
print("System ON" if system_on else "System OFF")
time.sleep(0.5)
if system_on:
raw = ldr.read_u16()
v = raw / 65535 * 5
r = 2000 * v / (1 - v / 5)
lux = pow((50000 * pow(10, 0.7) / r), (1 / 0.7))
print("Light Intensity: {:.2f} lux".format(lux))
if lux > 300:
led.off()
buzzer.off()
print("Bright Area")
elif lux > 100:
led.on()
buzzer.off()
print("Dim Light")
else:
print("Intruder Alert Mode")
led.on()
buzzer.on()
time.sleep(0.2)
buzzer.off()
time.sleep(0.2)
buzzer.on()
time.sleep(0.2)
buzzer.off()
else:
led.off()
buzzer.off()
time.sleep(0.5)