How to run a sub program twice using condition within another routine

Hi, I am struggling with one particular instance in my code. I have made a circuit using arduino uno which during day time, using LDR to sense sunrise and switches off the balcony lights automatically. Simultaneously after some gap, it operates one pump from a reservoir tank to water my plants for certain time frame. After this, the light remains off but the watering of plants do not repeat. During sundown, the ldr based circuit and arduino switches on the lights in balcony and again the watering pump starts for certain time frame, after which it stops, but the light remains on. This repeats everyday.

My first half of the program runs perfectly but during night time, the pump is getting on after certain interval again and again. Can someone help me which part is causing this to happen?

Code is here : Automated Plant Watering and garden lighting

Note: Am not good with coding so excuse my codes

I can't see your sketch from here.

Please don’t make us hunt for your information, display it here with your question.

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Did you have produced spaghetti code ?

Thank you, the code below is working so far. Will give it another 24 hours to check.

#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 > 301000UL)//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;
digitalWrite (relay2, HIGH); // Light is now on
while (blockingFlag == true && millis()-previousMillis > 301000UL)
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);

I don't need 24h.
That will kill all realtime design goals.

I would make the "turn the lights ON" level different from the "turn the light OFF" level:

  if (lightvalue <= 90) // Condition for day time
    // Daytime
    digitalWrite (LightOnPin, LOW); // Light is now off

  if (lightvalue >= 70) // Condition for night time
    // Nighttime
    digitalWrite (LightOnPin, HIGH); // Light is now on

How long should the pump run at sunrise?

How long should the pump run after sunset?

1 Like

Thank you with the help. I changed the lightvalue to different values in condition check for sunrise and sunset pump operation. Also I have set pump on time for 70 seconds after sunrise and 30 seconds after sunset. The value 30*1000UL came as 301000UL. Its working now.

In the code, its 15*1000UL, somehow after coping it here, the * symbol gets deleted.

Thanks, I will do so from onwards. I am new to this forum and learning the basics. I will post a schematic from onwards.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.