Time sensitive loop for night light

Hello,

I have an idea, that I make a nightlight(with photoresistor, and LEDs) and an Arduino Clock without RTC, and the nighlight works from night to morning. The clock part is OK, it’s not accurate yet, but in this case it’s not important.
My main problem with the project, that I cannot make a loop that works from a time to a given time, and inside the loop the tasks run just once.
So I would like to ask some advice for the loop handling.

I put my code here, I know it’s far from good, but after I learn the basic principles, I would like to do a lot of correction:

nightlight2.ino (808 Bytes)

int lightPin=0;
int ledPin=8;

int hr=12;
int mins=0;
int sc=0-1;

int photoValue=0;

void setup() {
  pinMode(lightPin,OUTPUT);
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(photoValue);
  sc++;
  Serial.print("Time:");
  Serial.print(hr);
  Serial.print(":");
  Serial.print(mins);
  Serial.print(":");
  Serial.println(sc);
  delay(1000);
  if(sc == 59){
    sc=0-1;
    mins++;
  }
  if(mins==60){
      mins=0;
      hr++;    
  }
  if(hr==24){
      hr=0;
  }
   if((sc == 30) && (mins == 0) && (hr == 12)){
     while((sc <= 45) && (mins <= 0) && (hr <= 12)){   
        photoValue=analogRead(lightPin);
        if(photoValue<600){
          digitalWrite(ledPin,HIGH);
        }else{digitalWrite(ledPin,LOW);}
      sc++;
      mins++;
      hr++;
    }
  }
}

Have a look at the blink without delay and use of millis don’t bother with hours , mins etc - just know how many milliseconds you want for your “on” time. So set a variable to the value of millis when you switch on , and when millis becomes greater than the original value plus the “on” time, then switch it off.

Use the time library, it will help you keep track of hours, minutes and seconds. Much easier. You just have to set it one time, and it'll keep the time as long as you don't power down (or put in deep sleep) your Arduino.