So basically I have a little circuit that has an RGB LED that when it get dark out (photosensor) the light comes on and fades through the spectrum.
The algorithm is basically:
void loop() {
if (it's dark out) {
fade through the whole spectrum}
else (if it's light out){
keep LED off}
}
In order to make it more power efficient during the day when it's light out, I wanted to add a delay in the else statement of 2 minutes, so it is not constantly rechecking multiple times a second. So my else statement looks like this:
else
{ //open else (if LIGHT)
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs.
delay(12000); //This is the line in question
} //close else
} //close void loop
But it appears not to be working. To test it I let the program run in the dark, turned on the house light. When it gets to the end of the cycle (red) it re-detects light and turns off. Then I turn off the house light and the LED immediately comes back on, so clearly it is not waiting 2 minutes. I've even waited up to 30 seconds after the LED has turned off because it detected light to turn off the house light to make sure it would be in that 2 minute time period.
I have attached the entire code below. Can anyone figure out what I am doing wrong?
#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 120
int ldr = 1; //Define photosensor in Analog port 1
int lightinput = 0; //Default value for lightinput is zero
int trigger = 250; //Threshold value of light to trigger activation.
//I have found between 200 and 600 works best. You don't want it so
//sensitive that it detects itself and flickers at the end of it's
//cycle.
void setup() {
pinMode(GREEN, OUTPUT); //define as digital outputs
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
}
int redVal;
int blueVal;
int greenVal;
void loop() {
lightinput = analogRead(ldr); //difine lightinput as value from photosensor
if (lightinput < trigger) //compare value to threshold trigger
{ //open if statement (if DARK)
int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );
delay( delayTime );
}
redVal = 0;
blueVal = 0;
greenVal = 255;
for( int i = 0 ; i < 255 ; i += 1 ){
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );
delay( delayTime );
}
redVal = 0;
blueVal = 255;
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );
delay( delayTime );
}
} //close if statement
else
{ //open else (if LIGHT)
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
delay(12000); //This code is supposed to save energy by only detecting
//light every two minutes as opposed to constantly, however it does not
//work.
} //close else
} //close void loop