#include <Servo.h>                  //Includes the Servo Library

Servo servo;                          //Creates a servo object

int moisture;                         //Creates the moisture variable (a value between 0-1000)
const int power_pin = 2;              //Defines all the pins we will use
const int sensor_pin = A0;
const int threshold = 500;            //We will need a threshold to know when we should water the plant
const int servo_pin = 2;

void setup() {
  Serial.begin(9600);                 //Allows for serial communication
  
  pinMode(power_pin, OUTPUT);         //Defines the Pins
  pinMode(sensor_pin, INPUT);

  servo.attach(servo_pin) ;           //Defines the servo pin
  servo.write(0);                     //Closes the valve
}

void loop() {
  digitalWrite(power_pin, HIGH);      //To reduce the wear on the moisture sensor, we will only turn on the sensor all 60s
  delay(5);                       
  moisture = analogRead(sensor_pin);  //Reads the signal of the sensor pin, which is the moisture sensor
  digitalWrite(power_pin, LOW);       //Turns off the sensor

  if(moisture < threshold) {          //If we have a moisture level under the threshold,
    servo.write(90);                  //we will open the valve for
    delay((500/moisture) * 1000)      //(500: moisuture) seconds, if we have a moisture level of 500, we will open the valve for 1 second
    servo.write(0):                   //Closes the valve
  }

  serial.println("The moisture of you plant is: ")
  serial.print(moisture);             //Prints the moisutre level of the plant
  
  delay(60000);
}

//Code by NotADesigner :)
