ATtiny sketch with sleep modes mis-behaving

I have a sketch for an ATtiny85 that compiles but does not perform as expected and I am at a loss for why. I am posting both schematic and sketch here. My circuit is battery powered and uses a ATtiny85 to light an LED array in dark conditions controlled by an LDR. LED brightness is controlled by PWM that is adjustable by a pot. PWM is increased as battery voltage decreases. To save power the sketch uses ATtiny85 sleep modes and drives the brightness pot. Problem the circuit only lights the LED array if booted in dark conditions, and then will turn off the LEDs in ambient light. However if booted in ambient light it will not turn on LEDs after transitioning to dark. I know that the sketch detects the dark condition because quiescent current changes from 65 microA to 35 microA when shifting from light to dark conditions. I cannot find the logical error that prevents lighting the LEDs when transitioning from light to dark. I have ruled out reaching LDR threshold values by measuring voltage in light and dark. I am a newbie and have used a variety of LLMs to help with the code and to sort this out but they are not up to the task. Any help is greatly appreciated`Use code tags to format code for the forum

```cpp
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <math.h>

// --- Light Thresholds ---
const uint16_t DARK_THRESHOLD_ON = 740;
const uint16_t DARK_THRESHOLD_OFF = 700;
const uint16_t DAYLIGHT_THRESHOLD_ON = 600;
const uint16_t DAYLIGHT_THRESHOLD_OFF = 700;

// --- Pin Assignments ---
const uint8_t LDR_ADC_PIN = A1;     // PB2 (Pin 7)
const uint8_t LDR_COMP_PIN = 1;     // PB1 (Pin 6)
const uint8_t BRIGHTNESS_PIN = A3;  // PB3 (Pin 2)
const uint8_t PWM_OUTPUT_PIN = 4;   // PB4 (Pin 3)
const uint8_t RV2_POWER_PIN = 0;    // PB0 (Pin 5)

bool isLedOn = false;
volatile bool wokeFromComparator = false;

// --- PWM Compensation Constants ---
const float REG_SLOPE = 1104.8f;
const float REG_INTERCEPT = -1849.7f;
const float VCC_NOMINAL_VOLTS = 2.94f;
const float I_MAX_AT_VNOMINAL = REG_SLOPE * VCC_NOMINAL_VOLTS + REG_INTERCEPT;
const float MIN_MULTIPLIER = 0.5f;
const float MAX_MULTIPLIER = 7.0f;

// --- Interrupt Handlers ---
ISR(WDT_vect) {}
ISR(ANA_COMP_vect) {
  wokeFromComparator = true;
}

// --- Utility Functions ---
float getCompensatedMultiplier(uint16_t vcc_mv_actual) {
  float vcc_volts = vcc_mv_actual / 1000.0f;
  float i_actual = REG_SLOPE * vcc_volts + REG_INTERCEPT;
  if (i_actual <= 1.0f) return MAX_MULTIPLIER;
  float multiplier = I_MAX_AT_VNOMINAL / i_actual;
  return fmaxf(MIN_MULTIPLIER, fminf(MAX_MULTIPLIER, multiplier));
}

void setupWatchdog() {
  MCUSR &= ~(1 << WDRF);
  WDTCR |= (1 << WDCE) | (1 << WDE);
  WDTCR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0);  // ~8s
}

void disableWatchdog() {
  MCUSR &= ~(1 << WDRF);
  WDTCR &= ~((1 << WDIE) | (1 << WDE));
}

uint16_t readVcc() {
  uint8_t oldADMUX = ADMUX;
  ADMUX = 0x0C;  // Internal 1.1V reference to measure Vcc
  delay(10);
  ADCSRA |= (1 << ADSC);
  while (ADCSRA & (1 << ADSC))
    ;
  uint16_t adcResult = ADC;
  ADMUX = oldADMUX;
  return adcResult == 0 ? 3000 : (uint16_t)(1126400L / adcResult);
}

uint16_t readVcc_stable() {
  uint32_t sum = 0;
  for (uint8_t i = 0; i < 4; i++) {
    sum += readVcc();
    if (i < 3) delayMicroseconds(100);
  }
  return sum / 4;
}

uint16_t readLDR() {
  ADMUX = (0 << REFS0) | (LDR_ADC_PIN & 0x07);
  delay(10);
  ADCSRA |= (1 << ADSC);
  while (ADCSRA & (1 << ADSC))
    ;
  return ADC;
}

uint16_t readBrightnessPotWithPowerControl_stable() {
  uint32_t sum = 0;
  for (uint8_t i = 0; i < 4; i++) {
    pinMode(RV2_POWER_PIN, OUTPUT);
    digitalWrite(RV2_POWER_PIN, HIGH);
    delayMicroseconds(150);
    sum += analogRead(BRIGHTNESS_PIN);
    digitalWrite(RV2_POWER_PIN, LOW);
    pinMode(RV2_POWER_PIN, INPUT);  // High impedance to reduce leakage
    if (i < 3) delayMicroseconds(100);
  }
  return sum / 4;
}

void enterSleepWithLDRComparator() {
  ADCSRA &= ~(1 << ADEN);  // Disable ADC

  ACSR &= ~(1 << ACIE);  // Disable comparator interrupt
  ACSR |= (1 << ACI);    // Clear interrupt flag

  // Enable comparator: AIN1 vs 1.1V bandgap, rising edge
  ACSR = (1 << ACIE) | (1 << ACBG) | (1 << ACIS1);  // rising edge only

  wokeFromComparator = false;

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_bod_disable();
  sei();
  sleep_cpu();
  sleep_disable();

  ACSR &= ~(1 << ACIE);   // Disable comparator interrupt
  ADCSRA |= (1 << ADEN);  // Re-enable ADC

  delay(10000);  // 10-second delay after wake-up to ensure LDR settles
}

void setupADC() {
  ADMUX = (0 << REFS0);
  ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}

void setupPWM() {
  pinMode(PWM_OUTPUT_PIN, OUTPUT);
  analogWrite(PWM_OUTPUT_PIN, 0);
}

void setup() {
  power_timer0_disable();

  pinMode(RV2_POWER_PIN, OUTPUT);
  digitalWrite(RV2_POWER_PIN, LOW);
  pinMode(LDR_ADC_PIN, INPUT);
  pinMode(LDR_COMP_PIN, INPUT);
  pinMode(BRIGHTNESS_PIN, INPUT);

  setupADC();
  setupPWM();

  for (int i = 0; i < 3; i++) {
    readLDR();
    delay(10);
  }

  disableWatchdog();
  sei();

  uint16_t light = readLDR();
  if (light >= DARK_THRESHOLD_ON) {
    uint16_t brightnessRaw = readBrightnessPotWithPowerControl_stable();
    uint16_t pwmRaw = brightnessRaw >> 2;
    uint16_t vcc = readVcc_stable();
    float multiplier = getCompensatedMultiplier(vcc);
    uint16_t scaledPwm = (uint16_t)(pwmRaw * multiplier);
    if (scaledPwm == 0 && brightnessRaw > 4) scaledPwm = 1;
    if (scaledPwm > 255) scaledPwm = 255;
    analogWrite(PWM_OUTPUT_PIN, (uint8_t)scaledPwm);
    isLedOn = true;
    setupWatchdog();
  } else {
    analogWrite(PWM_OUTPUT_PIN, 0);
    isLedOn = false;
    disableWatchdog();
    enterSleepWithLDRComparator();
  }
}

void loop() {
  uint16_t light = readLDR();
  uint16_t brightnessRaw = readBrightnessPotWithPowerControl_stable();
  uint16_t pwmRaw = brightnessRaw >> 2;
  uint16_t vcc = readVcc_stable();
  float multiplier = getCompensatedMultiplier(vcc);

  uint16_t scaledPwm = (uint16_t)(pwmRaw * multiplier);
  if (scaledPwm == 0 && brightnessRaw > 4) scaledPwm = 1;
  if (scaledPwm > 255) scaledPwm = 255;
  uint8_t pwmValue = (uint8_t)scaledPwm;

  if (!isLedOn) {
    if (light >= DARK_THRESHOLD_ON) {
      analogWrite(PWM_OUTPUT_PIN, pwmValue);
      isLedOn = true;
      setupWatchdog();
    } else {
      analogWrite(PWM_OUTPUT_PIN, 0);
      isLedOn = false;
      disableWatchdog();
      enterSleepWithLDRComparator();
      return;
    }
  } else {
    if (light <= DAYLIGHT_THRESHOLD_ON || (light <= DARK_THRESHOLD_OFF && light > DAYLIGHT_THRESHOLD_ON)) {
      analogWrite(PWM_OUTPUT_PIN, 0);
      isLedOn = false;
      disableWatchdog();
      enterSleepWithLDRComparator();
      return;
    } else {
      analogWrite(PWM_OUTPUT_PIN, pwmValue);
    }
  }

  if (isLedOn) {
    ADCSRA &= ~(1 << ADEN);
    set_sleep_mode(SLEEP_MODE_IDLE);
    sleep_enable();
    sleep_cpu();
    sleep_disable();
    ADCSRA |= (1 << ADEN);
  }
}

You should be powering the MCU, sensor and pot with a voltage regulated power supply, not from the LED power supply.

The power saved by using sleep modes is completely negligible compared to the power drawn by the LEDs.

Sorry I should have pointed out that the LED array will be very low brightness - testing showed 250 microA is the desired current. Objective is to run on AA alkaline batteries for roughly 9-12 months.

Sorry, I can't make much sense of the chatGPT code. The bot doesn't understand it either, otherwise it could provide useful comments.

Use your multimeter to measure the average current draw for the project. Estimated battery life, assuming 2500 mAh for AA cells is

Rough lifetime in hours = (2500 mAh)/(average current draw in mA)

when LEDs are lit total avg current is 600microA and of that LEDs are 250microA. at 2500milliAh for the batteries and guessing 12 h per day battery life is estimated to be 347 days.

Don't know what you are smoking, 9-12 hrs would be a great accomplishment with what you have, 9-12 months is a pipe dream.

I think you have confused uA for mA, that is a 1,000 times error.

The OP's math is correct. A circuit with average current draw of 600 uA will in theory run continuously for roughly

2500 mAh/0.6 mA = 4167 h or 173 days

That is your problem right there. Your code seems way way overly complicated for such a simple task. No wonder you can't figure out what is wrong.

If dark, LEDs on if light, LEDs off

if the LED is Not on, why do you need logic to to set isLedOn = false?

why isn't the logic determined more directly from 2 thresholds

    if (light >= THRESHOLD_ON) {
        isLedOn = true;
    }
    else if (light <= THRESHOLD_OFF)  {
        isLedOn = false;
    }

replace isLedOn = true; with whatever logic you want to control its intensity

the above obviously has a dead zone, hysterisis, between the 2 thresholds

You have certainly packed a stack of feature in that code such a brightness compensation for battery degrading, using the analog comparator to force a wakeup etc. There are also some unusual features like the design of the ambient light level measurement.

Is this correct:
There is only one problem with your code and that is if the device is powered up when the ambient light is high, it never responds if the ambient light subsequently drops low. Otherwise it performs correctly.

I could guess that, if it starts up when the light is high, a peripheral is switched off which prevents it detecting darkness.

Normally, you'd build the code incrementally starting with the basic functions like detecting the ambient light level and switching the LEDs, then adding features and testing at each stage.

Analog comparator is disabled in power down, so your code will never work.
See post #9

I'd suggest adding a led for debugging so you know what parts of the program are being executed, otherwise you are tapping in the dark, and the first place I'd blink it is in setup() then at the beginning of the loop().

You are using all the MCU pins already but PB0 can be freed with a minimum disturbance to the rest of the project. Simply connect RV2 pin 3 directly to Vcc instead of PB0. Comment out all lines of code which refer to RV2_POWER_PIN

Now add something like: const uint8_t DEBUG_LED = 0; // PB0 (Pin 5) as a global.
In setup, pinMode( DEBUG_LED, OUTPUT);
Then to blink the led at a specific point in the code, just add the following lines:

digitalWrite(DEBUG_LED, HIGH);
delay( 500 ) ;  // can vary this so you know which instance was invoked
digitalWrite(DEBUG_LED, LOW);

Apart from the other troubleshooting ideas you have been given, try reducing this delay(10000); // 10-second delay after wake-up to ensure LDR settles to say 5 seconds. I haven't followed all the logic but 10 seconds looks high if the watchdog timer triggers at 8 seconds.

no confusion, the LEDs are very dim for a darkened room. Current was measured by multimeter.

I can try shifting RV2 power back to Vcc for debugging with LEDs -thanks for that idea. It was shifted from Vcc to PB0 to give a really big power savings.

Come back in 9 months and let us know how many batteries you have used.

Have a look at a few videos re measuring very low currents.
https://youtu.be/LUB8RWzzLWc?si=EKcT1plwa0xPOkIl
Even better get this one (Current Ranger)
https://youtu.be/HmXfyLyN38c?si=5NEY51u5kvOtNvpj

See post #12
Your code will never work!

According to the Atmel ATtiny25/45/85 [DATASHEET] section 7.1.3 Power-down Mode "a pin change interrupt can wake up the MCU."

else used to enter a sleep function