A loop want to wait until a condition become true

i have 2 pushbuttons and one ultrasonic sensor. when i press button 1 then led 1 want to glow and ultrasonic sensor want to wait until detect something in 15cm range. if ultrasonic sensor detect something the relays want to ON. in my code when i press button and at the same time ultrasonic sensor detect something relays are ON. please help me to solve this problem.

here is my code..

const int relay1 = 10;
const int relay2 = 11;
const int trig = 7;
const int echo = 6;
int btn1 = 8;
int btn2 = 9;
int btlight1 = 2;
int btlight2 = 3;
boolean bt1g = true;
boolean bt2g = true;
const int distance = 15; //centrimeters
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

float duration_us , distance_cm;


void setup() {
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(btlight1, OUTPUT);
  pinMode(btlight2, OUTPUT);

  lcd.init();
  lcd.backlight();

}

void loop() {

  digitalWrite(btlight1, HIGH);
  digitalWrite(btlight2, HIGH);
  delay(100);
  digitalWrite(btlight1, LOW);
  digitalWrite(btlight2, LOW);
  delay(100);

  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);





  //// BTN1 ////

  //if btn1 is on//


  digitalWrite(btlight1, HIGH);
  digitalWrite(btlight2, LOW);

  if ( digitalRead(btn1) == HIGH ) {


    // generate 10s pulse to trig pin //
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);

    // measure duaration of pulse from echo pin//
    duration_us = pulseIn(echo, HIGH);

    // calculate the distance //
    distance_cm = 0.017 * duration_us;

    if ( distance_cm < distance ) {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, LOW);
      Serial.print("btn1:");
      Serial.println(1);
      delay(5000);
      bt1g = false;
    }
  }

  //if btn1 is off//


  digitalWrite(btlight1, LOW);

  if ( digitalRead(btn1) == LOW ) {


    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    Serial.print("btn1:");
    Serial.println(0);
    bt1g = true;
  }


  ////BTN2////

  //if btn2 is on//


  digitalWrite(btlight2, HIGH);
  digitalWrite(btlight1, LOW);

  if ( digitalRead(btn2) == HIGH ) {



    // generate 10s pulse to trig pin //
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);

    // measure duaration of pulse from echo pin//
    duration_us = pulseIn(echo, HIGH);

    // calculate the distance //
    distance_cm = 0.017 * duration_us;

    if ( distance_cm < distance ) {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, LOW);
      Serial.print("btn2:");
      Serial.println(1);
      bt1g = false;
    }
  }

  //if btn2 is off//


  digitalWrite(btlight2, LOW);

  if ( digitalRead(btn2) == LOW ) {


    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    Serial.print("btn2:");
    Serial.println(0);
    bt2g = true;
  }
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi Kohilan,
you should re-edit your first post. Below your post there is a pencil-icon

Click on that pencil-icon to activate edit-mode
There is a method to insert your complete sketch with a few mouse-clicks and keypresses

So as a first step simply delete all the code in your first posting

Then change to your Arduino-IDE and do this:

Then just paste the clipboard-content into the posting. By pressing Ctrl-V.
All formatting details are added automatically.

and you are done

best regards Stefan

I see your comment, but I do not see any code to detect if the switch is off. Perhaps there is something missing.

Please do not post pictures of code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.