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?
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 CtrlT or CMDT to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
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
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 (701000UL); //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 > 301000UL)
{
digitalWrite (relay1, HIGH); // Water pump is on
previousMillis = millis();
delay (151000UL); // Watering continues for 15 seconds
digitalWrite (relay1, LOW); // Water pump is off
}
blockingFlag = false;
}
delay (500);
}
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
}
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.