Repeating code until condition is true

Hi, I am working on a sketch that has two if loops. See code below:

#define led_bottom 19                             //led pin
int long unsigned time_1;                         //initial time
int long unsigned time_2;                         //final time
int long unsigned Time;                           //Delta Time

void setup () {
  Serial.begin(9600); 
  pinMode(led_bottom, INPUT);                     //Set bottom led pin as input
  digitalWrite(led_bottom, HIGH);
}
void loop() {
  if (digitalRead(led_bottom)==HIGH) {            //Checks if bottom led is high
    time_1=micros();                              //If high, takes time measurement
    delay(30);                                    //delay for bouncing
  }
  if(digitalRead(led_bottom)==LOW) {              //Checks if bottom led is LOW
    time_2=micros();                              //If low, takes time measurement  
    delay(30);                                    //delay for bouncing
  }
  Time=time_2-time_1;
   Serial.print("T1= ");                         //Text to monitor
   Serial.println(time_1);                       //prints time 1
    Serial.print("T2= ");                      
    Serial.println(time_2);                      //Prints time 2
    Serial.print("Delta T=  ");
    Serial.println(Time);                        //Prints final-initial timme
}

My question is, how can i get the first if statement to wait until the condition is true before it moves on, I need the second if statement to do the same.

See while.

Paul

Hi, I am working on a sketch that has two if loops.

No, it has two if STATEMENTS. If statements do not loop.

yanamada22:
Hi, I am working on a sketch that has two if loops. See code below:

#define led_bottom 19                             //led pin

int long unsigned time_1;                         //initial time
int long unsigned time_2;                         //final time
int long unsigned Time;                           //Delta Time

void setup () {
  Serial.begin(9600);
  pinMode(led_bottom, INPUT);                     //Set bottom led pin as input
  digitalWrite(led_bottom, HIGH);
}
void loop() {
  if (digitalRead(led_bottom)==HIGH) {            //Checks if bottom led is high
    time_1=micros();                              //If high, takes time measurement
    delay(30);                                    //delay for bouncing
  }
  if(digitalRead(led_bottom)==LOW) {              //Checks if bottom led is LOW
    time_2=micros();                              //If low, takes time measurement 
    delay(30);                                    //delay for bouncing
  }
  Time=time_2-time_1;
   Serial.print("T1= ");                         //Text to monitor
   Serial.println(time_1);                       //prints time 1
    Serial.print("T2= ");                     
    Serial.println(time_2);                      //Prints time 2
    Serial.print("Delta T=  ");
    Serial.println(Time);                        //Prints final-initial timme
}




My question is, how can i get the first if statement to wait until the condition is true before it moves on, I need the second if statement to do the same.

You can put a line with return after the delays.

PaulRB:
See while.

Paul

Thanks Paul, worked perfectly.

Glad to see that you have got it working. As you have not posted your code I can't see exactly what you have done but are 2 tests necessary ?
digitalRead(led_bottom)is going to return either HIGH or LOW, so if it is not HIGH it must be LOW and vice versa. Hence only one test is needed.

Your code has bigger problems than that. What are you actually trying to do.

The first time though loop, you calculate 'Time" although one or both of the values in that calculation may have an indeterminate value.