Hello,
I am a newbie and trying to make a plant watering system using Arduino UNO. Below is the code I used.
When I first switch on power, it works fine. After that it never turns on the pump even if the soil is dry. But if I switch it off and back on, it works for one more time. I tried to read the serial monitor, but the problem is, when I connect the USB cable, it starts working again.
I have kept a delay of 5 hours and the power to the sensor module is OFF before it reaches the 5 hrs to minimise corrosion of sensor probes.
I have also added a Cal switch to bypass the 5 hrs delay.
Any help is appreciated.. Thanks!!
void setup() {
Serial.begin(9600); // initializes serial communication at 9600 bits per second
pinMode(2, OUTPUT); // sets pins 2 to 5 for water pumps in output mode
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT); // Digital pin 6 for Cal switch
pinMode(7, OUTPUT); // sets pin 7 for powering sensors
}
void loop() {
digitalWrite(7, HIGH); // sets the digital pin 7 high, to power sensors
delay(2000); // waits for 2 seconds
int sensorValue1 = analogRead(A0); // read value of humidity sensor
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
int sensorValue4 = analogRead(A3);
Serial.println(sensorValue1); // prints value to serial
Serial.println(sensorValue2);
Serial.println(sensorValue3);
Serial.println(sensorValue4);
if(sensorValue1 > 450){ // Value to be adjusted based on actual testing
digitalWrite(2, HIGH); // turns pump1 on if moisture is less than set value
delay(500); // waits for 1sec
}
else{
digitalWrite(2, LOW); // does not turn on pump 1 if moisture level is sufficient
delay(500); // waits for 1sec
}
if(sensorValue2 > 450){ // Value to be adjusted based on actual testing
digitalWrite(3, HIGH); // turns pump2 on if moisture is less than set value
delay(500); // waits for 1sec
}
else{
digitalWrite(3, LOW); // does not turn on pump2 if moisture level is sufficient
delay(500); // waits for 1sec
}
if(sensorValue3 > 450){ // Value to be adjusted based on actual testing
digitalWrite(4, HIGH); // turns pump3 on if moisture is less than set value
delay(500); // waits for 1sec
}
else{
digitalWrite(4, LOW); // does not turn on pump3 if moisture level is sufficient
delay(500); // waits for 1sec
}
if(sensorValue4 > 450){ // Value to be adjusted based on actual testing
digitalWrite(5, HIGH); // turns pump4 on if moisture is less than set value
delay(500); // waits for 1sec
}
else{
digitalWrite(5, LOW); // does not turn on pump4 if moisture level is sufficient
delay(500); // waits for 1sec
}
delay(5000); // waits for 5 seconds
digitalWrite(7, LOW); // sets the digital pin 7 off to turn off sensors
digitalWrite(2, LOW); // Turns off all pumps
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
if (digitalRead (6) == HIGH) {
delay(18000000); // waits for 1 hour before taking next measurement
}
else{
delay(0);
}
}