[text] Automated Garden light and plant watering code

Viewer

copydownloadembedprintName: Automated Garden light and plant watering code
  1. #include <elapsedMillis.h>
  2. int relay1 = 13;//Water pump
  3. int ldr = A0;// Taking analog reading of LDR
  4. int relay2 = 8;//Led chain
  5. int lightvalue = 0;
  6. unsigned long previousMillis=0;
  7. bool blockingFlag = true;// Flag to use in condition
  8.  
  9. void setup() 
  10. {
  11.   pinMode (relay1, OUTPUT);
  12.   pinMode (relay2, OUTPUT);
  13. }
  14.  
  15. void loop() 
  16. {
  17. lightvalue = analogRead (ldr);
  18. lightvalue = 100 - lightvalue/10.24;
  19.  
  20. if (lightvalue <= 90)//Condition for day time
  21. {
  22.   digitalWrite (relay2, LOW); // Light is now off
  23.   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
  24.   {
  25.     digitalWrite (relay1, HIGH); // Water pump is on
  26.     previousMillis = millis();
  27.     delay (70*1000UL); //Watering continues for 70 seconds
  28.     digitalWrite (relay1, LOW); // Water pump is off
  29.   }
  30.   blockingFlag = true;
  31. }
  32. else
  33. {
  34.     digitalWrite (relay2, HIGH); // Light is now on
  35.     while (blockingFlag == true && millis()-previousMillis > 30*1000UL)
  36.     {
  37.     digitalWrite (relay1, HIGH); // Water pump is on
  38.     previousMillis = millis();
  39.     delay (15*1000UL); // Watering continues for 15 seconds
  40.     digitalWrite (relay1, LOW); // Water pump is off 
  41.     }
  42.     blockingFlag = false;
  43. }
  44. delay (500);
  45. }

Editor

You can edit this paste and save as new:


File Description
  • Automated Garden light and plant watering code
  • Paste Code
  • 31 May-2021
  • 1.4 Kb
You can Share it: