Updating the sketch to add photoresistor readings to the decision-making

Now we will update our sketch to ensure that we only water the plants when there is enough sunlight:

/* Gardening system
* Lets water our plants on time
*/

// humidity sensor is at analog port 0
const int SENSOR_PORT = A0;
const int PHOTO_RESISTOR = A1;
#define RELAY_OUTPUT 9

int sensorValue = 0;

void setup() {
// We will initialize the serial port so we can print humidity data to the serial port.
Serial.begin(9600);
pinMode(RELAY_OUTPUT, OUTPUT); // Set the pin to digital output mode
digitalWrite(RELAY_OUTPUT,LOW);
}

// the loop routine runs over and over again forever:
void loop() {
// Read the input on analog pin 0:
sensorValue = analogRead(SENSOR_PORT);
// A well watered soil will give a reading of atleast 380 to 400, a fully moist soil will have reading of 1023.
sensorValue = constrain(sensorValue, 400, 1023);
// We will reverse the range so that we get a rough humidity percentage reading i.e.
// fully moist soil will give a reading of 100% for 100% humidity and
// dry soil will read 0% for 0% humidity
sensorValue = map(sensorValue,400,1023,100,0);

// Print humidity value
Serial.println(sensorValue);

// Ensure that there is some ambient light before turning the pump on
if (sensorValue <= 20 && analogRead(PHOTO_RESISTOR) > 25) {
Serial.println("soil is dry. switching on the PUMP");
digitalWrite(RELAY_OUTPUT,HIGH);
delay(300000);
} else
{
Serial.println("soil is wet");
digitalWrite(RELAY_OUTPUT,LOW);
}

delay(1000);
}

This sketch reads the photoresistor and uses that as an additional condition to ensure that your plants are only watered during the day. We have set the light threshold as 25, but you may try to experiment with different light values to see what works best for you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.202.187