How to continue loop until condition is met

Hello guys! I just need some help.

I want to continue the loop in my if statement

if(tm.Hour == Hour && tm.Minute == Min && tm.Second == Sec)
    turnon(timeon);

until I get the right range of value of my pHsensor of 7.4-7.6. How can I do that?

Thank you in advance for the help.

#include <Wire.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

#define pump 8
#define Clvalve 9
#define H2Ovalve 10

int Hour=21;
int Min=28;
int Sec=30;

int timeon=60000;

#define pHSensor 0

unsigned long int avgValue;
int buf[10],temp;

#define trig 6
#define echo 7

void turnon(int timeon)
{

  //First line code of pH
  
   for(int i=0;i<10;i++)
  {
    buf[i]=analogRead(pHSensor);
    delay(10);
  }
  for(int i=0;i<9;i++)
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)
  avgValue+=buf[i];
  float pHValue=(float)avgValue*5.0/1024/6;
  pHValue=3.5*pHValue;

  //last line code of pH


//first line ultrasonic code
  float duration, distance;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) *0.0344;
//last line ultrasonic code
  
  if (pHValue >= 7.4)
  {
    Serial.println("pH Value HIGH!");
    Serial.print("pH:");
    Serial.print(pHValue,2);
    Serial.println("");
    Serial.print("Pump Open!");
    Serial.println();
    digitalWrite(pump,LOW);
      
      
      if (distance >= 12)
      {
        Serial.print("Distance = ");
        Serial.print(distance);
        Serial.println(" cm");
        digitalWrite(pump,HIGH);
        Serial.println("Pump Close!");
        Serial.println("Chlorine Valve Open!");
        digitalWrite(Clvalve, LOW);
        delay(4000);
        digitalWrite(Clvalve, HIGH);
        Serial.println("Chlorine Valve Close!");
        Serial.println("Water Valve Open!");
        digitalWrite(H2Ovalve, LOW);
        delay(4000);
        digitalWrite(H2Ovalve, HIGH);
        Serial.println("Water Valve Close!");
        Serial.println();
      }
  }
  else if (pHValue <7.4)
  {
    Serial.println("pHValue LOW!");
    Serial.print("pH:");
    Serial.print(pHValue,2);
    Serial.println("");
    Serial.print("Chlorine Valve Open!");
    Serial.println();
    digitalWrite(Clvalve,LOW);
    delay(4000);
    digitalWrite(Clvalve,HIGH);
    Serial.println("Chlorine Valve Close!");
    Serial.println("Water Valve Open!");
    digitalWrite(H2Ovalve, LOW);
    delay(4000);
    digitalWrite(H2Ovalve, HIGH);
    Serial.println("Water Valve Close!");
    Serial.println();
  }
}

void setup() 
{
  Serial.begin(9600);

  while (!Serial);

  pinMode(pump,OUTPUT); // pump for drain =Normally Closed
  digitalWrite(pump,HIGH);
  
  pinMode(Clvalve, OUTPUT); // valve of chlorine=Normally closed 
  digitalWrite(Clvalve, HIGH);
  
  pinMode(H2Ovalve,OUTPUT); //valve of water = Normally closed
  digitalWrite(H2Ovalve,HIGH);

  pinMode(trig, OUTPUT); // ultrasonic sensor
  pinMode(echo, INPUT);

}

void loop() 
{
  ultrasonic();
  pHsensor();
  tmElements_t tm;
  

  if (RTC.read(tm)) 
  {
    Serial.print("Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
    if(tm.Hour == Hour && tm.Minute == Min && tm.Second == Sec)
    turnon(timeon);
  }
  delay(1000);
}

void pHsensor()
{
  for(int i=0;i<10;i++)
  {
    buf[i]=analogRead(pHSensor);
    delay(10);
  }
  for(int i=0;i<9;i++)
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)
  avgValue+=buf[i];
  float pHValue=(float)avgValue*5.0/1024/6;
  pHValue=3.5*pHValue;
  Serial.print(" pH:");
  Serial.print(pHValue,2);
  Serial.println(" ");
}


void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void ultrasonic()
{
   
  float duration, distance;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) *0.0344;
}

Have you looked up while command?

while (x == 2) {
x ++
}

makes the mini loop run until x is 2

I've tried the while command but nothing happened when I run the code.

What you’re trying to achieve is that the ‘process’ starts at a given time and ends when the pH has reached a certain value?

If so, you can do this using an additional variable. To give you the idea

void loop()
{
  static bool run = false;

  ...
  ...

  if (tm.Hour == Hour && tm.Minute == Min && tm.Second == Sec)
  {
    run = true;
  }

  if (pH > 7.4 && pH < 7.6)
  {
    run = false;
  }

  if (run == true)
  {
    turnon(timeon);    
  }
}