Controlling Multiple Sensor with Bluetooth

Hello Everyone,
I am trying To build one project in which currently I can control 2 Ultrasonic Sensor with Bluetooth.
Now I have done all the thing but nothing is happening
Here is my codding

int trig = 2;
int echo = 3;
int trig1 = 4;
int echo1 = 5;

long duration;
long duration1;
int distance;
int distance1;
int l1 = 6;
int l2 = 7;


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

pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(l1, OUTPUT);
pinMode(l2, OUTPUT);

}  
  void loop()
{

  digitalWrite(trig, LOW);
  digitalWrite(trig1, LOW);

    delayMicroseconds(5);
    digitalWrite(trig, HIGH);
    digitalWrite(trig1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);
    digitalWrite(trig1, LOW);
  
    duration = pulseIn(echo, HIGH);
    distance = duration*0.034/2;
     
    duration1 = pulseIn(echo1, HIGH);
    distance1 = duration1*0.034/2;

    
    
   if(Serial.available()>0)
   {     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {

        case '1':
          if (distance <= 200){
               digitalWrite(l1, HIGH);
          }
          else {
            digitalWrite(l1,LOW);
          }
 break;
            
             case '2':
          if (distance1 <= 200){
               digitalWrite(l2, HIGH);
}
          else {
            digitalWrite(l2,LOW);
          }
 break;

  
            case '3':
           
               digitalWrite(l1,LOW);
 
 break;
            
             case '4':
               digitalWrite(l2, LOW);
 break;
  
            default : break;
      }
      Serial.println(data);
   }
   delay(50);
}

Now only one time my sensor is doing and after that it dose'nt loop also I can't control or use both ultrasonic sensor at the same time lets say get distance and turn on LED from both ultrasonic when case 1 and case 2 is turn on and turn of 1 ultrasonic when case 3 is implemented

Pls help me with this thanks in advance

This is unfriendly and has the opposite effect. Your initial post is 10m ago.
And you post the first bumping post that has no new information!

You have been online in this forum for a long time. So if you haven't learn how to modiffy postings now is the right time to learn it.

  • Delete your second post
  • RE-edit your first posting to add more information and add a fully written "All kinds of tips are very much appreciated" at the end of your post

code-version that makes visible what your code is doing

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// Take it for granted at the moment scroll down to void setup
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


int trig = 2;
int echo = 3;
int trig1 = 4;
int echo1 = 5;

long duration;
long duration1;
int distance;
int distance1;
int l1 = 6;
int l2 = 7;


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

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
}
void loop(){

  digitalWrite(trig, LOW);
  digitalWrite(trig1, LOW);

  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trig, LOW);
  digitalWrite(trig1, LOW);
  dbgi("trig0/1 LOW-HIGH-LOW done",5,500);

  duration = pulseIn(echo, HIGH);
  dbgi("pulseIn(echo done",6,500);
  distance = duration * 0.034 / 2;

  duration1 = pulseIn(echo1, HIGH);
  dbgi("pulseIn(echo1 done",7,500);
  distance1 = duration1 * 0.034 / 2;


  if (Serial.available() > 0) {
    char data = Serial.read(); // reading the data received from the bluetooth module
    dbgc("BT:",data);
    
    switch (data) {

      case '1':
        dbg("1:",distance);
        if (distance <= 200) {
          digitalWrite(l1, HIGH);
        }
        else {
          digitalWrite(l1, LOW);
        }
        break;

      case '2':
        dbg("2:",distance1);
        if (distance1 <= 200) {
          digitalWrite(l2, HIGH);
        }
        else {
          digitalWrite(l2, LOW);
        }
        break;


      case '3':
        dbg("3:11 LOW",0);
        digitalWrite(l1, LOW);

        break;

      case '4':
        dbg("4: 12 LOW",0);
        digitalWrite(l2, LOW);
        break;

      default : break;
    }
    Serial.println(data);
  }
  delay(50);
}
  • one issue is you can't operate more than ultrasonic sensor at the same time because the pulse from one will interfere with the others. and you should also allow some time (speed of sound i 1ft/msec) for the pulse from one to dissipate before generating another pulse.

  • you should also develop and test code in stages. so trying just using one sensor and printing its results. add a 1 sec delay to minimize the output. then add the 2nd sensor and when satisfied remove the 1 sec delay

  • rather than remove that other code during this testing, put an #if 0/#endif around it so that the compiler ignores it. then change the 0 to 1 when ready to test it again

Thanks and Sorry
But still the same problem is there

I got it

OK
Now lets say that I have to control IR Sensor and Ultrasonic Sensor with Bluetooth Here is the code

char dataIn = 'S';
char determinant;
char det;

int irled = 6;
int irbuz = 9;
int ir = 2;
int trig = 3;
int echo = 4;
long duration;
int distance;
int ultraled = 7;
int ultrabuz = 11;



void setup() {
      Serial.begin(9600);
   pinMode(irled,OUTPUT);
  pinMode(irbuz,OUTPUT);
  pinMode(ir,INPUT);
  pinMode(trig, OUTPUT);
pinMode(echo, INPUT);


pinMode(ultrabuz,OUTPUT);
pinMode(ultraled,OUTPUT);
  
}

void loop() {
 digitalWrite(trig, LOW);
    delayMicroseconds(5);
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);
  
    duration = pulseIn(echo, HIGH);
    distance = duration*0.034/2;
      
  det = check();
    switch (det){
      case 'A': 
   if (digitalRead(ir)== LOW){
    Serial.println("Alert Ir Detects Object");
    digitalWrite(irled , HIGH);
    digitalWrite(irbuz , HIGH);
     delay(10);
    }
      else
  digitalWrite(irled , LOW);
    digitalWrite(irbuz , LOW);
     delay(10);
          
det = check();
    break;

    
case 'C' :
  digitalWrite(irled , LOW);
    digitalWrite(irbuz , LOW);
      
det = check();
break;
    case 'E':
     if (distance <= 75){
    Serial.print(distance);
    Serial.println("cm  Extreme Risk ");
 digitalWrite(ultraled , HIGH);
 tone(ultrabuz,900);
  delay(900);
    digitalWrite(ultraled , LOW);
  noTone(ultrabuz);
  delay(900); 
     }

     else if (distance >= 76 && distance <=151){
     Serial.print(distance);
       Serial.println("cm  HIGH Risk");
digitalWrite(ultraled , HIGH);
  tone(ultrabuz,1400);
  delay(1400);
    digitalWrite(ultraled , LOW);
  noTone(ultrabuz);
  delay(1400);
  }

     else if (distance >= 152 && distance <=227){
    Serial.print(distance);
       Serial.println("cm  Medium Risk");
      digitalWrite(ultraled , HIGH);
  tone(ultrabuz,1500);
  delay(2000);
    digitalWrite(ultraled , LOW);
  noTone(ultrabuz);
  delay(2000);
     }
     
     else
     digitalWrite(ultraled,LOW);
     digitalWrite(ultrabuz,LOW);

 det = check();
    break;
    
    case 'F':
   digitalWrite(ultraled,LOW);
     digitalWrite(ultrabuz,LOW);

 det = check();
    break;

 
    }
}


int check(){
  if (Serial.available() > 0){// if there is valid data in the serial port
    dataIn = Serial.read();// stores data into a varialbe
if (dataIn == 'A'){//Forward
      determinant = 'A';
    }
    else if (dataIn == 'C'){//Backward
      determinant = 'C';
    }
    else if (dataIn == 'E'){//Backward
      determinant = 'E';
    }
    else if (dataIn == 'F'){//Backward
      determinant = 'F';
    }
      
    }
  return determinant;
}

This code I have done but now what is happening I am unable to turn on both sensor together mean to say that if I want to use IR as well as Ultrasonic sensor's together I am unbale to do that all things are going as expected except of this
Only 1 Sensor is working at the time If I want Ultrasnoic and IR then I can use only one

Pls Help me with this

Also please keep these in mind:-

  1. Code must loop
  2. Can use both sensor at the same time

I am stuck into this working from the morning but not getting resolution

Thanks in Advance
Krishna

First of all you should use autoformatting your code by pressing Ctrl-T on your keyboard.

Your code looks like this

   if (digitalRead(ir)== LOW){
    Serial.println("Alert Ir Detects Object");
    digitalWrite(irled , HIGH);
    digitalWrite(irbuz , HIGH);
     delay(10);
    }
      else
  digitalWrite(irled , LOW);
    digitalWrite(irbuz , LOW);
     delay(10);
          
det = check();
    break;

but it should look like this

      if (digitalRead(ir) == LOW) {
        Serial.println("Alert Ir Detects Object");
        digitalWrite(irled , HIGH);
        digitalWrite(irbuz , HIGH);
        delay(10);
      }
      else
        digitalWrite(irled , LOW);
      digitalWrite(irbuz , LOW);
      delay(10);

      det = check();
      break;

can you see the difference?
you have improper indention which makes it harder to understand your code

Ok Thanks for the information I was unaware of this
Here is the correction

char dataIn = 'S';
char determinant;
char det;

int irled = 6;
int irbuz = 9;
int ir = 2;
int trig = 3;
int echo = 4;
long duration;
int distance;
int ultraled = 7;
int ultrabuz = 11;



void setup() {
  Serial.begin(9600);
  pinMode(irled, OUTPUT);
  pinMode(irbuz, OUTPUT);
  pinMode(ir, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);


  pinMode(ultrabuz, OUTPUT);
  pinMode(ultraled, OUTPUT);

}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  det = check();
  switch (det) {
    case 'A':
      if (digitalRead(ir) == LOW) {
        Serial.println("Alert Ir Detects Object");
        digitalWrite(irled , HIGH);
        digitalWrite(irbuz , HIGH);
        delay(10);
      }
      else
        digitalWrite(irled , LOW);
      digitalWrite(irbuz , LOW);
      delay(10);

      det = check();
      break;


    case 'C' :
      digitalWrite(irled , LOW);
      digitalWrite(irbuz , LOW);

      det = check();
      break;
    case 'E':
      if (distance <= 75) {
        Serial.print(distance);
        Serial.println("cm  Extreme Risk ");
        digitalWrite(ultraled , HIGH);
        tone(ultrabuz, 900);
        delay(900);
        digitalWrite(ultraled , LOW);
        noTone(ultrabuz);
        delay(900);
      }

      else if (distance >= 76 && distance <= 151) {
        Serial.print(distance);
        Serial.println("cm  HIGH Risk");
        digitalWrite(ultraled , HIGH);
        tone(ultrabuz, 1400);
        delay(1400);
        digitalWrite(ultraled , LOW);
        noTone(ultrabuz);
        delay(1400);
      }

      else if (distance >= 152 && distance <= 227) {
        Serial.print(distance);
        Serial.println("cm  Medium Risk");
        digitalWrite(ultraled , HIGH);
        tone(ultrabuz, 1500);
        delay(2000);
        digitalWrite(ultraled , LOW);
        noTone(ultrabuz);
        delay(2000);
      }

      else
        digitalWrite(ultraled, LOW);
      digitalWrite(ultrabuz, LOW);

      det = check();
      break;

    case 'F':
      digitalWrite(ultraled, LOW);
      digitalWrite(ultrabuz, LOW);

      det = check();
      break;


  }
}


int check() {
  if (Serial.available() > 0) { // if there is valid data in the serial port
    dataIn = Serial.read();// stores data into a varialbe
    if (dataIn == 'A') { //Forward
      determinant = 'A';
    }
    else if (dataIn == 'C') { //Backward
      determinant = 'C';
    }
    else if (dataIn == 'E') { //Backward
      determinant = 'E';
    }
    else if (dataIn == 'F') { //Backward
      determinant = 'F';
    }

  }
  return determinant;
}

You are talking about "controlling" sensors

Sensors are not controlled. You read-in the sensor-signal.

A switch-case-statement used with break; at the end of each case (which is the usual way) means that each case is executed mutually exclusive.

The mutually exclusiveness stops doing two things at the same time.
Depending on how and when you want to do two things in parallel your code must look like very different.

As a somehow experienced user I can see that you have multiple misconceptions how coding works. You should stop tinkering around trying this, trying that.

write in normal words.

You should take 30 minutes time to write down a detailed description of your wanted functionality in normal words.

Similar to this example:

If I press "A" on my smartphone the smartphone sends the A to the arduino over bluetooth and then I want the arduino to do .....

If I press "B" on my smartphone the smartphone sends the B to the arduino over bluetooth and then I want the arduino to do .....

It is still unclear what kind of functionality you want to have.
So any suggested code could be different from what you want.

This is the reason why you should post a normally worded and detailed description of your wanted functionality

best regards Stefan

not sure what this is saying

  • neither sensor works
  • only one sensor works (which ?)
  • if one sensor is enabled (how?) the other doesn't work

i think the single (before there were two) ultrasonic sensor should work. don't see why the IR sensor should interfere with it

but did you review your code after reformating it? are braces missing in some of the else statements?

Give me some time will come up with proper code and brief description of my problem

Hi Once Again
Here I come up with all the brief things
First this is my complete Code

int irled = 6;
int irbuz = 9;
int ir = 2;
int trig = 3;
int echo = 4;
long duration;
int distance;
int ultraled = 7;
int ultrabuz = 11;

void setup() {
  Serial.begin(9600);
  pinMode(irled, OUTPUT);
  pinMode(irbuz, OUTPUT);
  pinMode(ir, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(ultrabuz, OUTPUT);
  pinMode(ultraled, OUTPUT);

}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  if (Serial.available() > 0)
  {
    char data = Serial.read(); // reading the data received from the bluetooth module
    switch (data)
    {
      case '1':
        if  (digitalRead(ir) == LOW) {
          Serial.println("Alert Ir Detects Object");
          digitalWrite(irled , HIGH);
          digitalWrite(irbuz , HIGH);
          delay(10);
        }
        else {
          digitalWrite(irled , LOW);
          digitalWrite(irbuz , LOW);
        }

        break;
      case '2':
        digitalWrite(irled , LOW);
        digitalWrite(irbuz , LOW);
        break;
      case '3':
        if (distance <= 75) {
          Serial.print(distance);
          Serial.println("cm  Extreme Risk ");
          digitalWrite(ultraled , HIGH);
          tone(ultrabuz, 900);
          delay(900);
          digitalWrite(ultraled , LOW);
          noTone(ultrabuz);
          delay(900);
        }

        else if (distance >= 76 && distance <= 151) {
          Serial.print(distance);
          Serial.println("cm  HIGH Risk");
          digitalWrite(ultraled , HIGH);
          tone(ultrabuz, 1400);
          delay(1400);
          digitalWrite(ultraled , LOW);
          noTone(ultrabuz);
          delay(1400);
        }

        else if (distance >= 152 && distance <= 227) {
          Serial.print(distance);
          Serial.println("cm  Medium Risk");
          digitalWrite(ultraled , HIGH);
          tone(ultrabuz, 1500);
          delay(2000);
          digitalWrite(ultraled , LOW);
          noTone(ultrabuz);
          delay(2000);
        }

        else {
          digitalWrite(ultraled, LOW);
          digitalWrite(ultrabuz, LOW);
        }

        break;
      case '4':
        digitalWrite(ultraled, LOW);
        digitalWrite(ultrabuz, LOW);

        break;

      default : break;
    }
    Serial.println(data);
  }
  delay(50);
}

So First I am explaining the codding

 int irled = 6;
 int irbuz = 9;
 int ir = 2;
 int trig = 3;
 int echo = 4;
 long duration;
 int distance;
 int ultraled = 7;
 int ultrabuz = 11;.

Here I am explaining Connection and Variables....


 void setup() {
   Serial.begin(9600);
   pinMode(irled, OUTPUT);
   pinMode(irbuz, OUTPUT);
   pinMode(ir, INPUT);
   pinMode(trig, OUTPUT);
   pinMode(echo, INPUT);
   pinMode(ultrabuz, OUTPUT);
   pinMode(ultraled, OUTPUT);
 
 }

Here I am starting up the program by Beginning Serial Monitor and Other Things

Now the main part Starts


  digitalWrite(trig, LOW);
   delayMicroseconds(5);
   digitalWrite(trig, HIGH);
   delayMicroseconds(10);
   digitalWrite(trig, LOW);
 
   duration = pulseIn(echo, HIGH);
   distance = duration * 0.034 / 2;

Code for Ultrasonic Sensor



   if (Serial.available()  0)
   {
     char data = Serial.read(); // reading the data received from the bluetooth module
     switch (data)
     {

Code for start reading data from Bluetooth


  case '1':
         if  (digitalRead(ir) == LOW) {
           Serial.println("Alert Ir Detects Object");
           digitalWrite(irled , HIGH);
           digitalWrite(irbuz , HIGH);
           delay(10);
         }
         else {
           digitalWrite(irled , LOW);
           digitalWrite(irbuz , LOW);
         }
 
         break;

When case 1 is been on by smartphone so this will happen
If the ir detects some thing so turn on the IRLED and IRBUZ also print on serial Monitor Alert IR.....


 case '2':
         digitalWrite(irled , LOW);
         digitalWrite(irbuz , LOW);
         break;

Here I am trying to do nothing just turn of IRLED and IRBUZ or we can also say that Easily turning of IR Sensor working of detection or sending signals


  case '3':
         if (distance <= 75) {
           Serial.print(distance);
           Serial.println("cm  Extreme Risk ");
           digitalWrite(ultraled , HIGH);
           tone(ultrabuz, 900);
           delay(900);
           digitalWrite(ultraled , LOW);
           noTone(ultrabuz);
           delay(900);
         }
 
         else if (distance = 76 && distance <= 151) {
           Serial.print(distance);
           Serial.println("cm  HIGH Risk");
           digitalWrite(ultraled , HIGH);
           tone(ultrabuz, 1400);
           delay(1400);
           digitalWrite(ultraled , LOW);
           noTone(ultrabuz);
           delay(1400);
         }
 
         else if (distance = 152 && distance <= 227) {
           Serial.print(distance);
           Serial.println("cm  Medium Risk");
           digitalWrite(ultraled , HIGH);
           tone(ultrabuz, 1500);
           delay(2000);
           digitalWrite(ultraled , LOW);
           noTone(ultrabuz);
           delay(2000);
         }
 
         else {
           digitalWrite(ultraled, LOW);
           digitalWrite(ultrabuz, LOW);
         }
 
         break;

Here if the case 3 is been sent by the smartphone so ultrasonic sensor start working and do as mentioned.


  case '4':
         digitalWrite(ultraled, LOW);
         digitalWrite(ultrabuz, LOW);
 
         break;

When Case 4 is been sent by the smartphone so just turn of the led and buzzer which was turning on by ultrasonic sensor.

So now here I am briefing what I want to do
I want to turn on case 1 and case 3 i.e. Ultrasonic sensor working as well as IR sensor working together and use the both sensor together want to say that if IR detects then turn on the assignd led and buzzer and at the same time if ultrasonic sensor detects it should also turn on and work as mentioned .

But here now what is happening I can sent only 1 case at a time lets say case 1 only I can't simulate both cases 1 and 3 at the same time.

This thing I have done in real world

But with the same type of code when I want to turn on LED's it is working as expected
Attaching the real world video
LED Controlled by Bluetooth

Here is the code for LED's

 void setup() {
Serial.begin(9600);
 pinMode(3, OUTPUT); // put your setup code here, to run once:
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 }

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available()>0)
   {     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {
        case '1': digitalWrite(3, HIGH);break; 
        case '2': digitalWrite(4, HIGH);break;         
        case '3': digitalWrite(5, HIGH);break;

        case '4': digitalWrite(3, LOW);break; 
        case '5': digitalWrite(4, LOW);break;         
        case '6': digitalWrite(5, LOW);break;
        
        default : break;
      }
      Serial.println(data);
   }
   delay(50);
}

So Now I fell like here I have explained myself completely.

Now Pls Help Me....

Thanks
Krishna

but you only process one input at a time. and it looks like case 2 turns off the irled and irbuz that may be set in case 1 and similar for case 4 that turns ultrled/buz off

and because you only check the distance in case '3', the ultraled/buz may remain on even though the sensor distance has changed


what would you like to happen when you received inputs '1' or '3'? these case aren't "enabling" those sensors, they are doing one-time processing of the sensors

I will help you with the program. Promised.
The following text is in Hindi language.

I don't know a single word of Hindi. All translation is from google-translate. This should show you how good google-translate is.

Either you speak Hindi yourself and can read this text directly or you are forced to use google-translate.

आपने जिस तरह से उत्तर दिया, उससे मुझे लगता है कि आप बहुत कम अंग्रेजी बोलते हैं।
इससे कई तरह की भ्रांतियां पैदा होती हैं।
इसलिए मैं यह पाठ जर्मन में लिख रहा हूं और गूगल-अनुवाद का हिंदी में अनुवाद करवा रहा हूं।

या तो आप स्वयं हिंदी बोलते हैं और इस पाठ को सीधे पढ़ सकते हैं या आप मजबूर होकर google-translate का उपयोग करते हैं।

आप अलग-अलग पात्रों को Arduino पर भेजते हैं।
तब केवल इस वर्ण से संबंधित आदेशों का मूल्यांकन किया जाता है।

फिर एक नया कैरेक्टर आता है और केवल नए कैरेक्टर के कमांड्स को ही एक्जीक्यूट किया जाता है।

आपको क्या लिखना चाहिए, इसका मेरा विवरण आपको समझ नहीं आया क्योंकि आप पर्याप्त अंग्रेजी नहीं बोलते हैं।

आपको सामान्य रोज़मर्रा के शब्दों का उपयोग करके यह वर्णन करना चाहिए कि आपका प्रोग्राम बिना कोड के कैसे काम करना चाहिए।

Your project will be finished much quicker if you take the trouble to write everything in your native language to DETAILED first and then let google-ranslate translate it into English.
best regards Stefan

That's what I am trying to explain that only one sensor is working at the time but I want both sensors to work together.
Could You pls help me over that if you want any other details, I am here I will respond to it ASAP

No that's not true basically what is happening I am in too hurry and also in too worry because of this but I have edited my post and now there is no typo's
As I have to translate your Hindi writing to English I came to understand "You should describe how your program should work without a code"
That's what I am asking, if I am missing something in code or I am writing some wrong in code.
Pls help me I am in big problem

Ok you want it short and in english:

add variables that define modes of operation one value for each mode of operation
then add a second switch case that executes all the code needed for each mode of operation.
Does this help?

Ya but I am still a bit confused Can you help me with some basic code example I know you all are there to help us with problem not with the codding but still please suggest me

do me a favor and write please instead of "pls"

you have multiple cases:

  1. only read ultrasonic sensor
  2. only read IR-senor
  3. read ultrasonic-sensor AND IR-sensor at the same time

first step is to describe ALL these mode of operations
by AVOIDING ALL programming-terms

pure functionality in total absence of any code

The code will come back very soon. But it will speed up finishing your project if you give an overview about all modes of operations that you want to have.