Help needed

Dear all,
I’m working on a sketch for an irrigation system, the operating principle consists of a humidity sensor, a pump and four valves. When the humidity level reaches a minimum, the pump and valve # 1 are turned on until the maximum level is reached, the operating time is recorded as an elapsed time period and its registration is used to operate the remaining valves one by one. Due to my lack of knowledge I have two problems, I am using the delay function (ms) and the sketch stops for a long time and the second problem is that the cycle of the subsequent valves is constantly repeated. Although I have reviewed the tutorial of the millis function and others, I have not been able to optimize the sketch.
I appreciate the help you can give me.
Best Regards

sketch_relay.ino (2.28 KB)

unsigned int value1 = 0;
int sensorPin = A0;

//relay operation
const int Relay1 = 4;

int relaysPin[] = {5,6,7} ; // an array of pin numbers to which Relays are attached
int pinCount = 3 ;           // the number of pins (i.e. the length of the array)


//elapsed time counter
unsigned long start, finished, elapsed;
int previous = LOW;
long time = 0;
long debounce = 5;
unsigned long previousMillis = 0;        // will store last time Relay was updated

//sensor operation range
int minVal=700; //value between 0 and 1023
int maxVal=900; //value between 0 and 1023

int Relay1State = LOW;
int pinState = LOW;


void displayResult()
{
  float h,m,s,ms;
  unsigned long over;
  elapsed=finished-start;
  h=int(elapsed/3600000);
  over=elapsed%3600000;
  m=int(over/60000);
  over=over%60000;
  s=int(over/1000);
  ms=over%1000;
  Serial.print("Raw elapsed time: ");
  Serial.println(elapsed);
  Serial.print("Elapsed time: ");
  Serial.print(h,0);
  Serial.print("h ");
  Serial.print(m,0);
  Serial.print("m ");
  Serial.print(s,0);
  Serial.print("s ");
  Serial.print(ms,0);
  Serial.println("ms");
  Serial.println();
  delay (100);
}

void setup() 
{
 
  // start serial
  Serial.begin(9600);

//relay cycle
  Serial.println("cycle duration");
  
  pinMode(sensorPin, INPUT);
  pinMode(Relay1, OUTPUT);
  
  // loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(relaysPin[thisPin], OUTPUT);
}
}
   
void loop() 
{

value1=analogRead(sensorPin);
 Serial.print ("value 1=  ");
 Serial.println (value1);
  
 // turn the Relay1 on

 if (value1>= maxVal)
 {
digitalWrite(Relay1, LOW);}

if (value1<= minVal)
{
digitalWrite(Relay1, HIGH);
}
//operate the other relays
if (digitalRead (Relay1) == LOW)
{
 for (int thisPin = 0; thisPin < pinCount; thisPin++) 
 {
      // turn the pins on:
      digitalWrite(relaysPin[thisPin], HIGH);
    delay(elapsed);
    // turn the pins off:
    digitalWrite(relaysPin[thisPin], LOW); 
    delay(10);
 }
}
 if(digitalRead(Relay1) == LOW){
//turn on millis
    start=millis();
    delay(5); // for debounce
    Serial.println("Started...");
    }
  if (digitalRead (Relay1) == HIGH)
  {
    time = millis();
    finished = millis();
    elapsed = finished - start;
    displayResult();
    delay (5);    
  }                     
 
}

You have a couple bad code things going on there.

You check if Value1 >= maxVal, then set Relay1 to LOW.

Then, you check twice more if Relay1 is LOW. Do all that code in one block, like this. . .

if (value1>= maxVal)
 {
   digitalWrite(Relay1, LOW);
   for (int thisPin = 0; thisPin < pinCount; thisPin++) 
    {
       // turn the pins on:
       digitalWrite(relaysPin[thisPin], HIGH);
       delay(elapsed);
       // turn the pins off:
       digitalWrite(relaysPin[thisPin], LOW); 
       delay(10);
    }
    //turn on millis
   start=millis();
   delay(5); // for debounce
   Serial.println("Started...");
 }
 else if (value1<= minVal)
 {
   digitalWrite(Relay1, HIGH);
   time = millis();
   finished = millis();
   elapsed = finished - start;
   displayResult();
   delay (5);    
}

One problem I see already is that it’s possible you call delay(elapsed) before elapsed has any meaningful value in it. It is called when value1 >= maxVal, but it isn’t set to anything until value1 <= minVal. This is complicated by the fact that elapsed isn’t set unti value1 <= minVal, but it uses “start” which isn’t defined until value1 >= maxVal.

Tighten up your code, and try to “step through” the loop paying particular attention to what the values are when you’re using “elapsed” and “start”.

For starters, everytime you go into one of those loops, perhaps do a Serial.println() on the variables of interest.

Thanks, it’s looks weird but the elapsed time code works ok, in my sketch the snippet works indefinitely

for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
// turn the pins on:
digitalWrite(relaysPin[thisPin], HIGH);
delay(elapsed);
// turn the pins off:
digitalWrite(relaysPin[thisPin], LOW);
delay(10);
}
and I need that runs once.