/* Soil Moisture Basic Example
This sketch was written by SparkFun Electronics
Joel Bartlett
August 31, 2015
Basic skecth to print out soil moisture values to the Serial Monitor
Released under the MIT License(http://opensource.org/licenses/MIT)
*/
// Code edited and changed by Miles Lewis
// added light sesor input as well as LED and servo output of values
#include <Servo.h>
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
int lightMeter = A1;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
Servo servo;
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.
void setup()
{
Serial.begin(115200); // open serial over USB
servo.attach(9);
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
pinMode(lightMeter, INPUT);
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
void loop()
{
Serial.print("Soil Moisture = ");
//get soil moisture value from the function below and print it
Serial.println(readSoil());
//This 1 second timefrme is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(500);//take a reading every second
int lightVal = analogRead(lightMeter);
Serial.println(lightVal);
if (lightVal <= 100){
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
Serial.println("All off");
}
else if (lightVal > 100 and lightVal <= 400){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
Serial.println("LED 1 is on");
}
else if (lightVal > 400 and lightVal <= 550){
digitalWrite(LED3, LOW);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
Serial.println("LED 2 is on");
}
else if (lightVal > 550){
digitalWrite(LED3, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
Serial.println("LED 3 is on");
}
}
//This is a function used to get the soil moisture content
int readSoil()
{
digitalWrite(soilPower, HIGH);//turn D7 "On"
delay(10);//wait 10 milliseconds
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
Serial.println("reading soil");
int servoPos = map(val,0,880,0,180);
servo.write(180-servoPos);
return val;//send current moisture value
}