from machine import Pin, ADC, PWM
import time
PIR_PIN = 15
LDR_PIN = 26
LED_PIN = 16
pir_sensor = Pin(PIR_PIN, Pin.IN)
ldr_sensor = ADC(Pin(LDR_PIN))
led = PWM(Pin(LED_PIN))
led.freq(1000)
ldr_threshold = 30000
def calculate_brightness(ldr_value):
if ldr_value < ldr_threshold:
return int((ldr_threshold - ldr_value) / ldr_threshold * 65535)
return 0
while True:
pir_value = pir_sensor.value()
ldr_value = ldr_sensor.read_u16()
if pir_value == 1:
for i in range(5):
led.duty_u16(65535)
print(f"Blinking LED: Iteration {i+1}, PIR Value: {pir_value}, LDR Value: {ldr_value}")
time.sleep(0.2)
led.duty_u16(0)
time.sleep(0.2)
else:
brightness = calculate_brightness(ldr_value)
led.duty_u16(brightness)
print(f"LDR Value: {ldr_value}, PIR Value: {pir_value}, LED Brightness: {brightness}")
time.sleep(0.1)