- #include <elapsedMillis.h>
- int relay1 = 13;//Water pump
- int ldr = A0;// Taking analog reading of LDR
- int relay2 = 8;//Led chain
- int lightvalue = 0;
- unsigned long previousMillis=0;
- bool blockingFlag = true;// Flag to use in condition
- void setup()
- {
- pinMode (relay1, OUTPUT);
- pinMode (relay2, OUTPUT);
- }
- void loop()
- {
- lightvalue = analogRead (ldr);
- lightvalue = 100 - lightvalue/10.24;
- if (lightvalue <= 90)//Condition for day time
- {
- digitalWrite (relay2, LOW); // Light is now off
- while (lightvalue <=90 && blockingFlag == false && millis()-previousMillis > 30*1000UL)//Since during sunset and sunrise the lightvalue fluctuates causing the loop to run multiple times, to prevent the pump from going on, a delay condition is set in the last using milis for 30 seconds
- {
- digitalWrite (relay1, HIGH); // Water pump is on
- previousMillis = millis();
- delay (70*1000UL); //Watering continues for 70 seconds
- digitalWrite (relay1, LOW); // Water pump is off
- }
- blockingFlag = true;
- }
- else
- {
- digitalWrite (relay2, HIGH); // Light is now on
- while (blockingFlag == true && millis()-previousMillis > 30*1000UL)
- {
- digitalWrite (relay1, HIGH); // Water pump is on
- previousMillis = millis();
- delay (15*1000UL); // Watering continues for 15 seconds
- digitalWrite (relay1, LOW); // Water pump is off
- }
- blockingFlag = false;
- }
- delay (500);
- }
[text] Automated Garden light and plant watering code
Viewer
Editor
You can edit this paste and save as new: