Back to Parent

//FINAL CODE

//declaring integers
int val = 0;//variable to store soil value
int soil = A0;//Declare a variable for the soil moisture sensor
int soilPower = D7;//Variable for Soil moisture Power
int motor = D1; // motor pin number
//We wil use a digital pin to power this sensor, to prevent oxidation
//of the sensor as it sits in the corrosive soil.
//Setup function for the application
void setup()
{
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
pinMode(motor, OUTPUT);//set D0 as an OUTPUT
digitalWrite(motor, LOW); //set motor off to start before read loop
//connect to wifi and cloud
WiFi.on();
Particle.connect();
//Creates variables that are exposed through the cloud.
//You can request values from these using a variety of methods
Particle.variable("soil", &val, INT);
Particle.variable("motor", &val, INT);
}
//loop for application (turn on motor when soil moisture is below 200)
void loop()
{
//display soil moisture to confirm is working
Serial.print("Soil Moisture = ");
//get soil moisture value from the readSoil function below and print it
Serial.println(readSoil());
delay(1000);//Take a reading every second
//This time is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to change this or increase it so
//you can take readings much less frequently.
    //If your soil is too dry, turn on motor to rotate feather
    //that notifies you with motion
    if(readSoil() < 200)
    {
      Serial.println("Motor Running?");
      // turn motor on if moisture level below 200
      digitalWrite(motor, HIGH);
    }
    else
    {
      // turn motor off 
      digitalWrite(motor, LOW);
    }
}
//This function is used to get the soil moisture content
int readSoil()
{
    digitalWrite(soilPower, HIGH);//turn D7 "On"
    delay(10);//wait 10 milliseconds
    val = analogRead(soil);
    digitalWrite(soilPower, LOW);//turn D7 "Off"
    return val;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0