int moistureReading = 0; int directionPin = 2; int powerPin = 3; bool isOpen = false; void setup() { Serial.begin(9600); pinMode(directionPin,OUTPUT); pinMode(powerPin,OUTPUT); delay(1000); } void loop() { moistureReading=analogRead(0); Serial.println(moistureReading); if(moistureReading<250) // is the soil dry? { if(!isOpen) //if the soil is dry and the drive is closed { digitalWrite(directionPin,HIGH); //switch to openning digitalWrite(powerPin,HIGH); //turn on the power Serial.println("Opening"); isOpen=true; } else //if the drive is already open { Serial.println("Opened"); digitalWrite(powerPin,LOW); //turn off the power } } else // if the soil is wet { if(isOpen) //if the soil is wet and the drive is already open { digitalWrite(directionPin,LOW); //switch to closing direction digitalWrite(powerPin,HIGH); //turn the power on Serial.println("Closing"); isOpen=false; } else //if the drive is already closed { Serial.println("Closed"); digitalWrite(powerPin,LOW); //turn the power off } } delay(1000); }