int sensorPin = A0; // select the input pin for the hygrometer int sensorValue = 0; // variable to store the value coming from the sensor int led = 12; // led indicator of watering int hyg = 7; // pin to turn the sensor on and off int k; int cnt =0; long betweentime = (8*3600000); //hour min second --every 8 hours long wateringtime = 5*1000; // 5 seconds for each burst of watering time void setup() { // declare the ledPin as an OUTPUT: pinMode(led, OUTPUT); pinMode(hyg, OUTPUT); Serial.begin(9600); } void loop() { k = mySensingFunction(); Serial.print("cnt = " ); Serial.print(cnt ); Serial.print(" , sensor = " ); Serial.println(k); //approx 330-1023, wet-dry if(k > 700){ // if it's dry- water loop until wet digitalWrite(led, HIGH); //turn on the LED delay(wateringtime); //water for 5 secs digitalWrite(led,LOW); cnt++; //add to the cnt } else { //if it's wet // subtract the amount of time was taken to water the plants from the eight hours long newtime = betweentime-(cnt*(wateringtime+1000)); Serial.print("newtime = " ); Serial.println(newtime ); if(newtime < 1){ newtime=5000; } delay(newtime); cnt = 0; } } int mySensingFunction(){ digitalWrite(hyg, HIGH); delay(1000); // read the value from the sensor: sensorValue = analogRead(sensorPin); digitalWrite(hyg,LOW); return sensorValue; }