0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« on: March 21, 2012, 09:31:24 pm » |
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
Brattain Member
Karma: 245
Posts: 16516
Available for Design & Build services
|
 |
« Reply #1 on: March 21, 2012, 09:45:37 pm » |
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
|
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 752
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #2 on: March 22, 2012, 05:22:01 am » |
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
Edison Member
Karma: 24
Posts: 2350
|
 |
« Reply #3 on: March 22, 2012, 06:41:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
WV
Offline
Full Member
Karma: 0
Posts: 160
Arduino, Helps With the ADD
|
 |
« Reply #4 on: March 22, 2012, 06:56:14 pm » |
Maybe something instead of a multimeter , you could debug VIA serial monitor. Reading your Values. Just a thought  Seems the best way to get reading from your sensors reguardless of type,resistor and so forth 
|
|
|
|
« Last Edit: March 22, 2012, 06:58:27 pm by woody_unreal »
|
Logged
|
|
|
|
|
WV
Offline
Full Member
Karma: 0
Posts: 160
Arduino, Helps With the ADD
|
 |
« Reply #5 on: March 25, 2012, 06:02:33 pm » |
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); }
|
|
|
|
« Last Edit: March 25, 2012, 06:07:06 pm by woody_unreal »
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 245
Posts: 16516
Available for Design & Build services
|
 |
« Reply #6 on: March 25, 2012, 07:27:57 pm » |
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
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #7 on: March 26, 2012, 03:32:37 am » |
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
Brattain Member
Karma: 245
Posts: 16516
Available for Design & Build services
|
 |
« Reply #8 on: March 26, 2012, 05:27:25 pm » |
Post your code so we are not debugging in the dark.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #9 on: March 26, 2012, 07:13:45 pm » |
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
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #10 on: March 26, 2012, 09:12:40 pm » |
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
|
|
|
|
|
|