// https://wokwi.com/projects/406570338257492993
// https://forum.arduino.cc/t/beginner-coding-for-automatic-irrigation-system/1292966

# define soilSensor A0
# define aSolenoid 2

int threshold = 500; // Change this value to adjust the threshold for activating the pump

void setup() {
  Serial.begin(115200);

  pinMode(aSolenoid, OUTPUT);
}

void loop() {

  int soil_moisture = analogRead(soilSensor) + random(100);
  soil_moisture = constrain(soil_moisture, 0, 1023);
  
  Serial.print("Soil Moisture: ");
  Serial.println(soil_moisture);

  if (soil_moisture < threshold - 200) {
    // Activate pump
    digitalWrite(aSolenoid, HIGH);
    Serial.println("Solenoid1 activate.");
  }
  
  if  (soil_moisture > threshold + 200) {
    // Deactivate pump
    digitalWrite(aSolenoid, LOW);
    Serial.println("sOLENOID1 not active.");
  }

  delay(333);
}