noiasca:
a) recap the flow again:
the blocking flag will be released only after the LDR will geht above a certain threshold. So if it gets dark, the plant gets watered once only. So it is basically a question how you set your two thresholds.
b) if you don't rely on the threshold only, consider something like robin2 explained. Add a time component.
when you are finished with watering, remember the actual timestamp
previousMillis=millis();
and add a conditon in your check that you want at least - let's say 18h difference
if (ldr > onThreshold && blockingFlag == false && millis()-previousMillis > 18601000UL)
edit, just as POC:
not tested with hardware:
// https://forum.arduino.cc/index.php?topic=713828.0
const uint16_t brightValue = 900;
const uint16_t darkValue = 200;
const uint8_t blockHours = 18; // minimum waiting time for next watering
bool blockingFlag = false; // watering not allowed
const uint8_t ldr = A0; // Pin with LDR
const uint8_t relay2 = 13; // Pin with active low relay
uint32_t previousMillis;
void setup()
{
Serial.begin(115200);
Serial.println(F("waters plants once after dark"));
Serial.println(F("send w for manual watering"));
pinMode(relay2, OUTPUT);
}
void loop()
{
int lightvalue = analogRead(ldr);
if (lightvalue > brightValue && blockingFlag == false && millis() - previousMillis > blockHours * 60 * 1000UL)
{
Serial.println(F("activated"));
doWatering();
blockingFlag = true;
}
if (lightvalue < darkValue)
{
Serial.println(F("reset blocking"));
blockingFlag = false;
}
if (millis() % 5000 == 0) // roughly/unsecure all 5 seconds...
{
Serial.print(F("last watering "));
Serial.print((millis() - previousMillis) / 60 / 1000UL);
Serial.print(F(" minutes ago. LDR="));
Serial.print(lightvalue);
if (blockingFlag)
Serial.println(F(" - blocked"));
else
Serial.println(F(" - active"));
}
if (Serial.available())
{
char c = Serial.read();
switch (c)
{
case 'w' :
doWatering();
break;
case '\r' : // just ignore CR
case '\n' : // just ignore LF
break;
default:
Serial.println(F("Command unknown"));
}
}
}
void doWatering()
{
Serial.println(F("watering - blocking"));
digitalWrite (relay2, LOW);
delay(4242); // dirty delay
digitalWrite (relay2, HIGH);
previousMillis = millis();
}
Hi, Thanks again. I could sum up to use your method and am able to stop water (relay1) in any particular instance. However, since I am changing the blockingFlag = true to stop the watering, it is not allowing me to start watering my plants in the next instance. The watering seems to stop after only one run. Can you please help with my below coding? I have kept the timings short to check in real time.
#include <elapsedMillis.h>
int relay1 = 13;
int ldr = A0;
int relay2 = 8;
int lightvalue = 0;
unsigned long previousMillis=0;
boolean blockingFlag = false;
void setup()
{
pinMode (relay1, OUTPUT);
pinMode (relay2, OUTPUT);
}
void loop()
{
lightvalue = analogRead (ldr);
lightvalue = 100 - lightvalue/10.24;
if (lightvalue >= 90)
{
digitalWrite (relay2, LOW);
if (lightvalue >=90 && blockingFlag == false && millis()-previousMillis > 21000UL)
{
digitalWrite (relay1, HIGH);
previousMillis = millis();
delay (5000);
digitalWrite (relay1, LOW);
blockingFlag = true;
}
}
else
{
digitalWrite (relay2, HIGH);
if (lightvalue <=90 && blockingFlag == false && millis()-previousMillis > 21000UL)
{
digitalWrite (relay1, HIGH);
previousMillis = millis();
delay (5000);
digitalWrite (relay1, LOW);
blockingFlag = true;
}
}
delay (500);
}