Loading...
Pages: [1]   Go Down
Author Topic: On dark turn Led on and off in an hr  (Read 609 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
   }
}   
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 245
Posts: 16516
Available for Design & Build services
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17

Portugal
Offline Offline
God Member
*****
Karma: 9
Posts: 752
Tomorrow I will know a BIT more than yesterday
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B


New Jersey
Offline Offline
Edison Member
*
Karma: 24
Posts: 2350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
#define LIGHTON 3600000
would be better as:
Code:
#define LIGHTON 3600000UL
Strange things happen when you use constant literals that won't fit in an int without their appropriate suffix.
Logged

WV
Offline Offline
Full Member
***
Karma: 0
Posts: 160
Arduino, Helps With the ADD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe something instead of a multimeter , you could debug VIA serial monitor. Reading your Values. Just a thought smiley
Seems the best way to get reading from your sensors reguardless of type,resistor and so forth smiley
« Last Edit: March 22, 2012, 06:58:27 pm by woody_unreal » Logged

WV
Offline Offline
Full Member
***
Karma: 0
Posts: 160
Arduino, Helps With the ADD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
/*
#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);
}  
« Last Edit: March 25, 2012, 06:07:06 pm by woody_unreal » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 245
Posts: 16516
Available for Design & Build services
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int waiting_for_day;

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 245
Posts: 16516
Available for Design & Build services
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}   
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 27, 2012, 02:31:54 am by hemant » Logged

Pages: [1]   Go Up
Print
 
Jump to: