How to interrupt a loop till a value is reached.

I’m developing an automatic watering system with an Arduino MEGA 2560.
The idea is that one pot at the time gets watered(10 in total). The pump is not strong enough to do more than one valve. Each pot has its own Soil Moister Sensor (Sparkfun), if the thresholdDown is <=300 a valve and 0.5 sec. later the pump will open/run (via relay’s). If the thresholdUp is >= 700 the pump- and valve relay's will switch off.
So far there are no errors in the sketch while compiling and uploading. In fact it works pretty nice. The problem is that the sketch keeps on going and not wait for the first valve to be finished.
I did try [if … else] Controle Structures as well as [Break] and [Continue], but didn’t had luck with these choices.

The idea is:
Messure pot 1, if dry(theresholdDown) --> valve and pump running
wait till it's wet (thresholdUp) --> pump and valve closed, move to pot 2
or
Messure pot 1, if wet (thresholdUp) --> do nothing, move to pot 2

After the last one the loop still may go on (When this works well, I will ad an RTC so it runs at a certain time).

So I do hope if anyone can help me out.

Here is a part of my code, the whole code wil be in the attachment:

void loop(){ 
                  
                    // set the relay's
                    //1st set Soil moisture sensors + relay's
                                      
  int sensorValue1;
    sensorValue1 = analogRead(sensorPin1);    //Reads SMSP value.
    if (sensorValue1 <= thresholdDown){
    digitalWrite(relayPin1, HIGH);            //solenoid valve1 open
    delay(500);                               //0.5 sec. delay between valve and pump
    digitalWrite(relayPin0, HIGH);            //solenoid Pump running
    }
    //sensorValue1 = analogRead(sensorPin1);    //Not necessary
    else if (sensorValue1 >= thresholdUp){         
    digitalWrite(relayPin0, LOW);             //solenoid Pump stopped
    delay(500);                               //0.5 sec. delay between valve and pump
    digitalWrite(relayPin1, LOW);             //solenoid valve1 closed
    //digitalWrite(sensorPin1, LOW);  //Actief maken natest met aansturing via NPN transistor
    }
    delay(500);                               //print value's every 0.5 sec.
    sensorValue1 = analogRead(sensorPin1);
    Serial.print("sensor1 = " );
    Serial.println(sensorValue1);

                    // end sensor 1

                    // 2nd set Soil moisture sensors

  int sensorValue2;
    sensorValue2 = analogRead(sensorPin2);  //Reads SMSP value.
    if (sensorValue2 <= thresholdDown){
    digitalWrite(relayPin2, HIGH);          //solenoid valve2 open
    delay(500);                             //0.5 sec. delay between valve and pump                         
    digitalWrite(relayPin0, HIGH);          //solenoid Pump running
    }
    //sensorValue2 = analogRead(sensorPin2);  //Not necessary
    else if (sensorValue2 >= thresholdUp){         
    digitalWrite(relayPin0, LOW);           //solenoid Pump stopped
    delay(500);                             //0.5 sec. delay between valve and pump
    digitalWrite(relayPin2, LOW);           //solenoid valve2 closed
    }
    delay(500);                             //print value's every 0.5 sec.
    sensorValue2 = analogRead(sensorPin2);
    Serial.print("sensor2 = " );
    Serial.println(sensorValue2);
              
                    // end sensor 2

Begin__code_sprinkler_mar10b.ino (6.22 KB)

I did try [if … else] Controle Structures as well as [Break] and [Continue], but didn't had luck with these choices.

The choice of control structures has nothing to do with writing blocking code.

The problem is that the sketch keeps on going and not wait for the first valve to be finished.

It would wait if you said something like

if(soilIsDry(1))
{
   startWatering(1);

   while(soilIsDry(1))
   {
   }

   stopWatering(1);
}

Where you provide functions named soilIsDry(), startWatering(), and stopWatering() that take a zone number, and that do what the name suggests that they should do.

Just watering for half a second may not be sufficient to get the water level up.

//sensorValue1 = analogRead(sensorPin1);  //Not necessary
//sensorValue2 = analogRead(sensorPin2);  //Not necessary

Why are these not necessary?

PaulS:
The choice of control structures has nothing to do with writing blocking code.
It would wait if you said something like

if(soilIsDry(1))

{
  startWatering(1);

while(soilIsDry(1))
  {
  }

stopWatering(1);
}



Where you provide functions named soilIsDry(), startWatering(), and stopWatering() that take a zone number, and that do what the name suggests that they should do.

Just watering for half a second may not be sufficient to get the water level up.

Thanks, this I would never figure out myself. I will start to play with this.
The half second pause a bit of safety, valve open first, then the pump starts. The watering has to stop when the moisture sensor reach the max. value I gave in.

boolrules:

//sensorValue1 = analogRead(sensorPin1);  //Not necessary

//sensorValue2 = analogRead(sensorPin2);  //Not necessary



Why are these not necessary?

I tried lot of things, then I found out that these lines didn't make a difference. I still left them there, just incase, as I am still 'fighting' with this code.