I'm trying to get a code working that will do the following;
Check to see if its dark using a light sensor
if its light, keep the LED off
if its dark, turn on the LED
If LED is on, keep LED on for 3 hours and then off for 9 hours
then back to checking if its dark.
So its for a light I want to come on automatically when it gets dark, then stay on for 3 hours and then stay off until the next day. I want it to ignore any light increases, i.e. when I turn other lights on, if this makes sense. This is what I have so far but its not up to the task.
The code works but not the 3 on 9 off timing
// These variables store the on and off pattern
// and the current state of the LED
int led = 2; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 10800000; // milliseconds of on-time
long OffTime = 32400000; // milliseconds of off-time
#include <RBD_LightSensor.h>
RBD::LightSensor light_sensor(A0);
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
int LS = (light_sensor.getPercentValue());
Serial.println(LS);
//check to see if its dark enought to turn the LED on
if (LS >= 40){
digitalWrite(led, LOW);
}
else {
digitalWrite(led, HIGH);
}
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(led, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(led, ledState); // Update the actual LED
}
}
...is still executing every time around the loop. So it is interfering with the code that is doing the timing and still turning the light on and off regardless..
Something more like (untested)....
//Check for start condition, set initial state...
if (running == false && LS < 40) {
running = true;
ledState = HIGH;
previousMillis = currentMillis; // Remember the time
}
//if timers active, wait for required times to expire, trigger state changes...
if (running) {
if(ledState == HIGH && currentMillis - previousMillis >= OnTime)
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
}
else if (ledState == LOW && currentMillis - previousMillis >= OffTime)
{
// REALLY? ARE YOU SURE YOU WANT TO END THE SEQUENCE WITH LED ON?
ledState = HIGH; // turn it on
// Not necessary. We’re not timing anything anymore, no need to remember time we stopped.
previousMillis = currentMillis; // Remember the time
running = false; //back to waiting for low light start condition
}
}
digitalWrite(led, ledState); // Update the actual LED
// These variables store the on and off pattern
// and the current state of the LED
int led = 2; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 10800000; // milliseconds of on-time
long OffTime = 32400000; // milliseconds of off-time
unsigned long startMillis;
unsigned long currentMillis;
#include <RBD_LightSensor.h>
RBD::LightSensor light_sensor(A0);
void setup() {
Serial.begin(9600);
startMillis = millis(); //initial start time
pinMode(led, OUTPUT);
}
void loop() {
int LS = (light_sensor.getPercentValue());
Serial.println(LS);
unsigned long currentMillis = millis();
//check to see if its dark enought to turn the LED on
//Check for start condition, set initial state...
if (led == LOW && LS <= 40) {
digitalWrite(led, LOW);
}
else if (led == LOW && LS > 40) {
digitalWrite(led, HIGH);
}
//if timers active, wait for required times to expire, trigger state changes...
else if (led == HIGH && LS > 40 && currentMillis - previousMillis >= OnTime) {
digitalWrite(led, LOW); // Turn it off
previousMillis = currentMillis; // Remember the time
}
else if (led == LOW && LS <= 40 && currentMillis - previousMillis >= OffTime)
{
digitalWrite(led, LOW); // Turn it off
}
}
Still having trouble with it starting and the 9 hours off.
The more I think about it, I think Delay would work best, I dont need to do anything while the light is On and Off.