On dark turn Led on and off in an hr

Hi
I have just started on Arduino
my project is to turn light when dark for 1hrs and turn off ,below is my code which dosent workway I want.instead on power LED is on and on dark after an hrs off.any help appriciated.
Thanks
CODE
#define LDR 0
#define LED 13
#define LIGHTON 3600000

unsigned long darkStart;
int ldrValue;

void setup(){
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}

void loop(){
ldrValue = analogRead(LDR);
if (ldrValue >= 912 ){
digitalWrite(LED, HIGH);
darkStart = millis();
}
if ( millis()-darkStart > LIGHTON){
digitalWrite(LED, LOW);
}
}

How do you have the LDR & LED wired up?
Sounds like maybe you want to look for
ldrValue<=912
to indicate dark?
And then add a flag so it does not come back until ldrValue has gone high to show it has been daylight again.

I could try using an multimeter on voltage mode to measure the voltage on the analog pin when you get dark on LDR.This way you will be chore about the value you have to expect on the if cicle. Like CrossRoads say you sould get a flag variable also

This:

#define LIGHTON 3600000

would be better as:

#define LIGHTON 3600000UL

Strange things happen when you use constant literals that won't fit in an int without their appropriate suffix.

Maybe something instead of a multimeter , you could debug VIA serial monitor. Reading your Values. Just a thought :slight_smile:
Seems the best way to get reading from your sensors reguardless of type,resistor and so forth :slight_smile:

OK i hope you dont mind i changed some of your code. I put in some Debugging and i have a Resistor on the input side just to bring down the values and changed the time for checking
It maybe nothing like you want. But maybe it will be in the right direction

/*
#define LDR A0
#define LED 13
#define LIGHTON 50
*/
int LDR = A0;
int LED = 13;
int LIGHTON = 50;
unsigned long darkStart;
int ldrValue;


void setup(){
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  pinMode(LDR, INPUT);
  Serial.begin(9600);
  }
   
void loop(){
  ldrValue = analogRead(LDR);
  if (ldrValue <= 512){
  digitalWrite(LED, HIGH);
   darkStart = millis();
  }
if ( millis()-darkStart > LIGHTON){  
   digitalWrite(LED, LOW);
   }
   Serial.println(ldrValue);
   Serial.println(LDR);
   delay(500);
}

So where is something that indicates you have been on for an hour, and are now waiting for daylight to occur?

i.e.
if (ldrValue<=512 && waiting_for_day == 0){ // finally dark out, and has not been daylight yet
turn on LED, start darkStart timer
waiting_for_day = 1; // don't start 1 hour again until its daylight
}
if ( (darkStart +duration)>=millis() ) {
turn off LED
}
if ( ldrValue >=512){
waiting_for_day = 0; // daytime, can look for nighttime now
}

int waiting_for_day;

Hi
sorry still din't work on dark led ON but dim and day time led OFF.
Regards

Post your code so we are not debugging in the dark.

Thanks for the help
after few corrections now it works.

  1. ldr value starts increasing which should not i think
  2. for a moment if ldr goes down below 512 and back to normal still the light turns on.

Thanks

code
#define LDR 0
#define LED 13
#define LIGHTON 60000 //Time light is on when dark

int waiting_for_day;
unsigned long darkStart;
int ldrValue;

void setup(){
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(9600);
}
void loop(){
ldrValue = analogRead(LDR);

if (ldrValue <= 512 && waiting_for_day == 0 ){
digitalWrite(LED, HIGH);
darkStart = millis();
waiting_for_day = 1;
}
if (millis()-darkStart > LIGHTON ){
digitalWrite(LED, LOW);
darkStart = 0;
}
if ( ldrValue >=512){
waiting_for_day = 0; // daytime, can look for nighttime now

}
Serial.println(ldrValue);
Serial.println(darkStart);
Serial.println(waiting_for_day);
delay(500);
}

After reducing R from 10k to 5k &
if ( ldrValue >=512){
waiting_for_day = 0; // daytime, can look for nighttime now
darkStart=0;
digitalWrite(LED, LOW);
it works
Pl. advise
Regards

Happy to know a better way.
Thanks