Problem with integrating delay code to activate waterpump

Hi All,

here my project and problems which I just can’t seem to figure out.

So the project is fairly simple. It will be a soil moisture control for my desk plant and so far for the very little knowledge I have after working only a few weeks with Arduinos and Electronics altogether, I think I came pretty far. The final setup hopefully one day looks like this:

  • Soil moisture sensor measures water content of soil maybe 2-3 times a day max.
  • Soil moisture lvl is displayed on an LCD screen.
  • When soil gets too dry, an alarm will sound and a solid blue LED will switch to red flashing LED.
  • A water pump is activated for a specific amount of time.
  • System resets, and returns to monitoring soil moisture.

So, I have the system pretty much done to the point where whenever the soil moisture lvl gets too low, the pump starts until the value reaches normal again. The alarm and flashing LED’s are working fine as well.

So here is the problem. When I try to tell him to only water for a specific time, it seems that as soon as the moisture lvl drops, the two tone alarm becomes one steady tone, the LCD looses it’s input and both LED’s turn off. It returns to normal monitoring mode though after the specified time and the Lcd shows the sensor readings again as well.

I have no clue how that little bit of code can interfere with the rest so much although it follows the same structure as the other code I put in as far as I can tell. Hope I kind of described the problem so that anyone can help solving it. Here is the code I use with the specified time for the water pump and a picture of the setup if that’s any help?:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused)


int thresholdUp = 550;
int thresholdDown = 250;
int sensorPin = A0;        // Soil moisture sensor
int soundPin = 3;  
int ledPin1 = 10;
int ledPin2 = 11;
int delayPeriod = 1;       // Delay for LED's
int waterPump = 13;        // Waterpump activated on pin 13
int wateringTime = 30000;  // My specified amount of time for watering


void setup()
{
  mySerial.begin(9600); 
  delay(500);           
  
pinMode (ledPin1, OUTPUT) ;
pinMode (ledPin2, OUTPUT) ;
pinMode (waterPump, OUTPUT) ;  // Activating pin 13
}

void loop() 
{
  String DisplayWords;
  
  int sensorValue;
  sensorValue = analogRead(sensorPin);
  
  mySerial.write(254); 
  mySerial.write(128);
  mySerial.write("                "); 
  mySerial.write("                ");

  
  mySerial.write(254); 
  mySerial.write(128);
  mySerial.write("Water Level: ");
  mySerial.print(sensorValue); 

  if (sensorValue <= thresholdDown)
  
  {
    mySerial.write(254); 
    mySerial.write(192);
    DisplayWords = "Water Lvl LOW!";
    mySerial.print(DisplayWords);
   
    tone (soundPin, 250);
    delay(500);

    digitalWrite (ledPin1, HIGH);
    delay (delayPeriod);
    digitalWrite (ledPin1, LOW);
    delay (delayPeriod);
    digitalWrite (ledPin2, LOW);

    digitalWrite (waterPump, HIGH);  // When soil moisture too low, activate pump
    delay (wateringTime);            // Watering time 30 Sec.
    digitalWrite (waterPump, LOW);   // Shut of water pump
   
 } 
 
    else if (sensorValue >= thresholdUp)
    
    {
    mySerial.write(254);
    mySerial.write(192);

    DisplayWords = "Water Lvl HIGH!";
    mySerial.print(DisplayWords);
    
    tone (soundPin, 250);
    delay(100);

    digitalWrite (ledPin1, HIGH);
    delay (delayPeriod);
    digitalWrite (ledPin1, LOW);
    delay (delayPeriod);
    digitalWrite (ledPin2, LOW);

    digitalWrite (waterPump, LOW);

  }
  
  else {
    mySerial.write(254); 
    mySerial.write(192);

    DisplayWords = "Water Lvl OKAY!";
    mySerial.print(DisplayWords);
    
    noTone (soundPin);

    digitalWrite (ledPin2, HIGH);
    digitalWrite (ledPin1, LOW);

    digitalWrite (waterPump, LOW);
    
  }

  delay(500); 
  
}

When you call delay you are stuck on that line waiting for time to pass before it moves on to the next line of code. That's called blocking. The rest of your code isn't running during that time. Instead of waiting for time to pass, you should rely on the fact that the loop function is repeating so you can just repeatedly check to see if enough time has passed. See the Blink Without Delay example for some inspiration.