# Corrected copy: defines the missing LED_PIN and fixes the task reference.
# The original uploaded ldr_dimmer.py is preserved separately.
# Original dimming behavior and configured values are unchanged.

from machine import Pin, PWM, ADC
import time

# GP numbers: change to match your wiring
LED_PIN = 15                # Fix: use GP15, as in the other LED scripts
LDR_PIN = 27                 # must be GP26, GP27, or GP28 (analog pins)

led = PWM(Pin(LED_PIN))
led.freq(1000)               # 1000 on/off cycles per second
ldr = ADC(LDR_PIN)

# Readings from Task 2: set these from your own room
BRIGHT_ROOM = 50000          # reading in normal room light
DARK_ROOM = 35000            # reading with the LDR covered

while True:
    light = ldr.read_u16()   # 0 (dark) to 65535 (bright)

    # 0.0 in a bright room, 1.0 in a dark room
    level = (BRIGHT_ROOM - light) / (BRIGHT_ROOM - DARK_ROOM)
    level = max(0, min(1, level))     # keep between 0 and 1

    led.duty_u16(int(level * 65535))  # darker room -> brighter LED
    print(light, round(level * 100), "%")
    time.sleep(0.1)
