/* Author: Shaun AKA Chillichump www.chillichump.com www.youtube.com/chillichump */ const String swName = "ChillichumpIO Ebb&Flood Basic"; const String swVer = "1.1"; const String swRel = "09/03/19"; /* Common Millis equivalents 4 hours = 14400000; 2 hours = 7200000; 1 hour = 3600000 10 minutes = 600000; 5 minutes = 300000; 2 minutes = 120000; 1 minute = 60000 */ const long floodIntMillis = 60000; //millis between flooding const long floodDurMillis = 5000; //millis for flood duration const long longestDelayBetweenFlooding = 43200000; //longest acceptable delay between flooding. This ensures flooding occurs even if lights fail etc. 43,200,000 is 12 hours const int lightLevel = 600; //this value indicates whether the sun is up. Value out of 1023. higher = lighter (indirect sun is around 850; normal indoor lighting is around 650) const long lightLevelDelay = 20000; //delay before deciding it is daylight. To avoid lights, torches etc. causing issues const byte pumpPin = 6; //pin number for activating relay for pump const byte pinLightSensor = A2; //pin number for light sensor unsigned int lightSensorNow = analogRead(A2); unsigned long sunUpMillis = 0; unsigned long sunDownMillis = 0; unsigned long pumpLastOn = floodIntMillis + lightLevelDelay; //ensuring pump comes on when device is turned on and it is light unsigned long pumpOffAt = 0; boolean sunUpCheck = false; boolean sunDownCheck = false; boolean sunUp; boolean lastState = false; void setup() { pinMode(pumpPin, OUTPUT); digitalWrite(pumpPin, LOW); //pump off } void loop() { lightSensorNow = analogRead(pinLightSensor); if (lightSensorNow >= lightLevel && sunUp == false) { sunUp = true; } if (lightSensorNow < lightLevel && sunUp == true) { sunUp = false; } if (sunUp == true && sunUpCheck == false) { //checking if sun is up or lights on sunUpMillis = millis(); sunUpCheck = true; } if (sunUp == false && sunDownCheck == false) { //checking if sun is down or lights are off sunDownMillis = millis(); sunDownCheck = true; } if (sunUp == true && millis() - sunUpMillis > lightLevelDelay && sunDownCheck == true) { //checking if sun has been up or lights have been on longer than lightLevelDelay sunDownCheck = false; } if (sunUp == false && millis() - sunDownMillis > lightLevelDelay && sunUpCheck == true) { //checking if sun has been down or lights have been off longer than lightLevelDelay sunUpCheck = false; } if (millis() - sunUpMillis > lightLevelDelay && sunUpCheck == true ) { //checking that there has been light longer than the lightLevelDelay if (millis() - pumpLastOn > floodIntMillis || millis() < pumpLastOn) { //checking when the pump was last on. also checking if the sun has just come up or lights are on to ensure a flooding at first light. pumpLastOn = millis(); pumpOffAt = millis() + floodDurMillis; //pumpOffAt has been added so that the pump doesn't switch off during a cycle if the lights go our or sun goes down } } if (millis() - pumpLastOn > longestDelayBetweenFlooding && millis() > longestDelayBetweenFlooding) { //ensuring there is water at least every x hours pumpLastOn = millis(); pumpOffAt = millis() + floodDurMillis; } if (millis() < pumpOffAt) { //turning the pump on digitalWrite(pumpPin, HIGH); //pump on } else { digitalWrite(pumpPin, LOW); //pump off } }