Ambient Light Sensor combined with on-off millis timings

Hi all,

Easy one but I have been looking at it too long and it has fried my head.

Have an LED set up on on/off timings using millis. But when the LED is in it's 'on' timing state i need it to be controlled by an ambient light sensor, so it could potentially be off even though it is in the 'on' timing section. then the ambient light sensor needs to have no effect on light state when in 'off' state mode.

here is where i am at


const unsigned int onTime = 5000;
const unsigned int offTime = 25000;


unsigned long previousMillis=0;


int interval = onTime;


boolean LED13state = true;

int temt6000Pin = A0; 

float light; 

int light_value; 


void setup() {
   pinMode(13, OUTPUT);
}

void loop() {
  
  int light_value = analogRead(temt6000Pin);

int switch_value = digitalRead(10);

light = light_value * 0.0976; 

Serial.println(light); 

   digitalWrite(13, LED13state);

   unsigned long currentMillis = millis();

   if ((unsigned long)(currentMillis - previousMillis) >= interval && light_value > 1000) {
      if (LED13state) {
         interval = offTime;
      } else {
         interval = onTime;
      }
      LED13state = !(LED13state);

      previousMillis = currentMillis;
   }
}

LED stays on/off for desired times, i just need to be able to control the brightness using ambient light sensor/photoresistor when it is on.

Thanks in advance.

What board are you using? Only a few, like the Due, support a variable brightness onboard LED in hardware. That seems to be what you're expecting, since pin 13 is the LED on Uno, Nano etc.

@fjr2612
Do not combine interval and ambient light conditions in one if()
Test the light value only if LED state is ON

The Uno, i suppose i'll have to add more leds and toggle on/off states to replicate variable brightness...

You can "bit bang" PWM but whether that is practical depends on what ever else you are doing... Why not just use the PWM capable pins with analogWrite()?