Hi,
Im not sure if this is the right section of the forum to be posting this in, but its the best fit i could find.
I have mad a "pee-light", it turns on a nice red glow in the hallway when i get up to pee in the middle of the night.
It has a:
- darlington photo transistor hooked up to pin A5
- "Electronic brick (seeed studios)" PIR hooked up to D11
- 2x LED's hooked up to D10
It tests for light, if it is dark then it tests for motion, if there is motion then it fades on the LED, if no motion detected for 9 seconds, it fades off LED's.
pretty simple, and i have it working exactly as i want it to, with the downside being battery life. I tried it on a 9vold battery and it only lasted 24-36 hours. ive since done some more testing and found out the following:
- it draws 22mA (both with the PIR off or on)
- it draws 47mA when LED's are at full brightness (for 9 seconds)
I have read that there are ways to sleep the arduino, but cant really work it out. it could really be off for hours at a time (while its light) but i can only find ways to sleep for 8 seconds.
please can some one suggest ways to improve battery life!??!
here is my code as it stands now. Im sure it looks horrific to all you, im only just starting and have not programmed before (apart from basic HTML 10 years ago!)
anyone has any suggestions on how to make it more neat or efficient that would be much appreciated too. But my main concern is for battery life.
int lightPin = 5; // analogue pin for testing ambient light
int lightReading; // value
int LEDpin = 10; // Pin for LEDs
int pirPin = 11; // Pin to read from PIR
int pirVal; // Returned PIR value
int brightness = 0; // how bright the LED is
int fadeAmount = 2; // how many points to fade the LED by
int fadeAmount2 = 4; // how many points to fade the LED by
int pirOnPin = 4; // Power to this pin is power to PIR
unsigned long timeon; // the ms val from millis() when LED is powered on
unsigned long timeoff; // calculated value time on plus 9 seconds
int lightstate = 0; // records what state the LED is in
void setup(){
//Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(pirOnPin, OUTPUT);
}
void loop(){
lightReading = analogRead(lightPin); // read light pin from photo transistor
//Serial.println(lightReading);
delay(500);
if(lightReading > 1000) { // if light is greater than 1000, eg dark, turn on PIR and strat reading
digitalWrite(pirOnPin, HIGH);
pirVal = digitalRead(pirPin);
//Serial.println(pirVal);
if(pirVal == HIGH){
//digitalWrite(13, HIGH); // used in testing to determine when the PIR was triggered
//delay(50);
//digitalWrite(13, LOW);
//Serial.println("fade in");
if(timeoff < millis()){ // check that the time the LEd are to be faded out is less than millis(). this stops the LED fading, going stright to off and fading back on on constantly
int i=0;
//digitalWrite(13, HIGH);
for(i=0; i < 255; i += fadeAmount2){ // fade LED on
analogWrite(LEDpin, i);
delay(50);
}
// Serial.println("full light");
//analogWrite(LEDpin, 255);
lightstate = 1; // set lighstate to "on", so that lights cant be faded off when they are off already.
}
timeon = millis(); // set the time the LED came on to full power
timeoff = timeon + 9000; // set the time the LED will fade off. this will be done each time there is motion detected.
}
else{
if(lightstate == 1){ // make sure LED is on
if(timeoff < millis()){ // LED has been on for 9 seconds
//Serial.println("fade out");
// digitalWrite(13, LOW);
int i = 255;
for(i=255; i > 0; i -= fadeAmount){ // fade off
analogWrite(LEDpin, i-1);
delay(50);
}
//Serial.println("light off");
//analogWrite(LEDpin, 0);
lightstate = 0; // LED state is off
}
}
}
//Serial.println(timeon);
//Serial.println(timeoff);
//Serial.println(millis());
}
else {
digitalWrite(pirOnPin, LOW); // ambient light has come back up (morning, or hall way light)turn off PIR
if(lightstate == 1){ // if the LEDs are on, fade off regardless of 9 seconds
//Serial.println("fade out");
int i = 255;
for(i=255; i > 0; i -= fadeAmount){
analogWrite(LEDpin, i-1);
delay(50);
}
//Serial.println("light off");
lightstate = 0; // reset variables
timeoff = 0;
}
}
}
Thanks for looking,
Gr0p3r