Thanks guys that was great. I'm glad i asked.
I went with an integer value that increments when the test shows the light is bright, and decrements when the light is low. It is held within an upper and lower limit.
The main problem was that while testing the light value there needs to be a delay before testing again. During this delay i wanted to be able to continue to function.
So i chose to use the millis() function to test every few seconds or so and then increment or decrement the value.
Then the code loops through different scenarios based on the current value.
Pretty messy probably..
So i take it most of you use a BOOLEAN type logic state that you flip during the day/night testing function? Then when looping through the other code, make decisions based on the current BOOLEAN state?
I think I went overboard and i really have a double HYSTERISIS going on, i will probably drop the integer value in favour of the BOOLEAN type state?
/*
Day night IR sensor shield 11/08/12 Trobb
Please enjoy my dodgy code!
I am not a programmer by any means!
Feel free to improve on it!
Check out http://www.tobyrobb.com
Written with Arduino IDE V.1.0.1
DEBUG VERSION!!!!
*/
#include <avr/wdt.h>
#define ldr A0 // LDR light sensor
#define ir A1 // IR movement sensor pin
#define timerPot A2 // Pin the timer potentiometer is connected to
#define watchdogPin 3 // Uncomment this line to have a pin blink to let us know the code is running
#define led 3 // Output for LED
#define gain 10 // the amount of gain to add to the timerPot 10 is a normal value
int luxVal = 150; // Value to trigger the low light condition
float timerValue; // A variable to hold the value of the delay on time
float timerDelay;
int dark = 0;
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to check the LDR (milliseconds)
void setup() {
// initialize the IO.
// pinMode(watchdogPin, OUTPUT); // Uncomment this line to have a different pin blink to let us know the code is running
pinMode(led, OUTPUT);
pinMode(ir, INPUT);
pinMode(ldr, INPUT);
digitalWrite(led, LOW); // start with LED off
Serial.begin(9600);
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
// Uncomment this section to have a pin blink to let us know the code is running
// blink some morse code OK
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(1500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(250);
digitalWrite(watchdogPin, LOW);
delay(500);
digitalWrite(watchdogPin, HIGH); // blink watchdog led
delay(500);
digitalWrite(watchdogPin, LOW);
Serial.println("Setup complete");
}
void loop() {
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
// Lets print some debugging values to the console
Serial.print("Current ldr reading is ");
Serial.println(analogRead(ldr));
Serial.print("Current sensor value ");
Serial.println(digitalRead(ir));
Serial.print("Current timer value is ");
Serial.println(analogRead(timerPot));
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
// Now check for darkness or daylight and movement
checkLdr();
// so long as its dark
while(dark <=0){
Serial.println("Its dark");
// and movement is detected
if(digitalRead(ir) == 1){
Serial.println("Movement detected");
digitalWrite(led, HIGH); // turn led on
Serial.println("LED is ON");
// now leave light on for a predetermined time
timerValue = analogRead(timerPot);
timerDelay = timerValue * (timerValue / gain); // times the value by itself divided by gain
Serial.println(timerDelay);
float i = timerDelay; // set timer variable
while(i >=0){
i --;
Serial.print("Timing out ");
Serial.println(i);
checkLdr(); // run the the check LDR routine to see if it is still dark.
if(dark<=0){
return;
}
wdt_reset(); //make sure we reset watchdog timer within the loop to prevent endless resetting
}
}
else{
Serial.println("No movement"); // no movement detected so turn led off
digitalWrite(led, LOW); // turn led off
checkLdr();
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}
}
// not dark enough yet
Serial.println("still not dark enough");
digitalWrite(led, LOW); // turn led off
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}
void checkLdr(){
// check to see if it's time to read the LDR;
// difference between the current time and last time
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you checked the LDR
previousMillis = currentMillis;
Serial.println("Checking LDR");
if(analogRead(ldr)>=luxVal){
dark++;
}
if(analogRead(ldr)<=luxVal){
dark--;
}
if(dark>=5){
dark = 5;
}
if(dark <=-5){
dark = -5;
}
}
Serial.print("Current dark reading is ");
Serial.println(dark);
}