I am trying to get my garden lights to turn on for varying durations after sunset.
I am using a LDR to fetch the light status however I would like to youse the millis() function to keep the LEDs on for a certain period while still being able to dim the circuit etc.
I feel like I am pretty close and have looked at blink without delay sketches and watched a bunch of videos but still can't quite crack it.
any help much appreciated
Ps. be gentle. I have been tinkering with arduino for 2 weeks
// Sets the Constants
int ldr = A0; //Set LDR PIN
int ldr_value = 0;
int GARDENLIGHTS = 5; //The PWM pin the LED is attached to
int PATHLIGHTS = 6; //The PWM pin the LED is attached to
int STATUS_LED_G = 7; //The Green Indicator LED of the outdoor Circuit
int STATUS_LED_R = 8; //The Red Indicator LED of the outdoor Circuit
unsigned long previousMillis = 0; //Will store the time that the lights were last turned on
static unsigned long two_hours = 72000000; // interval at which to Keep lights on for 2h (milliseconds)
const long four_hours = 14400000; // interval at which to Keep lights on for 4h (milliseconds)
const long six_hours = 21600000; // interval at which to Keep lights on for 6h (milliseconds)
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
// declares named LED pins to be an outputs://
pinMode(GARDENLIGHTS, OUTPUT);
pinMode(PATHLIGHTS, OUTPUT);
pinMode(STATUS_LED_G, OUTPUT);
pinMode(STATUS_LED_R, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
ldr_value=analogRead(ldr); //Reads the Value of LDR(light).
//Runs this code if the value of the LDR is less than #
if(ldr_value<500){
if (currentMillis - previousMillis < two_hours)
digitalWrite(STATUS_LED_G,HIGH); //Makes the Green Status LED glow in Dark.
digitalWrite(STATUS_LED_R,LOW); //Makes the Red Status LED turn off in the dark
/*digitalWrite(STATUS_LED_G,LOW); //Makes the Green Status LED glow in Dark.
digitalWrite(STATUS_LED_R,HIGH); //Makes the Red Status LED turn off in the dark*/
int analogValue = analogRead(A1); //Reads the input on analog pin A1 (value between 0 and 1023)
int brightness = map(analogValue, 0, 1023, 0, 255); //Scales it to brightness (value between 0 and 255)
analogWrite(GARDENLIGHTS, brightness); // sets the brightness of OUTDOOR LED 1 that connects to pin 5 through PWM
analogWrite(PATHLIGHTS, brightness); // sets the brightness of OUTDOOR LED 1 that connects to pin 5 through PWM
//print out the value of the garden
Serial.print("Analog: ");
Serial.print(analogValue);
Serial.print(", Brightness: ");
Serial.println(brightness);
previousMillis = currentMillis;
}
else //Runs this code if the value of the LDR is less than #
{
digitalWrite(STATUS_LED_G,LOW); //Turns the LED OFF in Light.
digitalWrite(STATUS_LED_R,HIGH); //Turns the LED ON in Light.
digitalWrite(GARDENLIGHTS,LOW); //Turns the LED OFF in Light.
Serial.println("LDR: "); //Prints the value of LDR to Serial Monitor.
Serial.println(ldr_value); //As a value.
}
}