Working with 2 Ultrasonic sensors

#define echo1 D0
#define echo2 D1
int alarm = 0; 
long distance1;
int duration1;
long distance2;
int duration2;
  Serial.begin(9600);

  pinMode(echo1, INPUT);
  pinMode(echo2, INPUT);
 //take input from echo
 
  //calculation 
  duration1 = pulseIn(echo1, HIGH);
  //calculation 
  distance1 = duration1 * 0.034 / 2;
  Serial.print("distance1 = ");
  Serial.println(distance1);
  /*
   * Second input
   */

  //take input from echo

  //calculation 
   duration2 = pulseIn(echo2, HIGH);
   distance2 = duration2 * 0.034 / 2; 
   Serial.print("distance2 = ");
   Serial.println(distance2);
       if(distance2 < 20){
      delay(50);
      if(distance1 < 20){
        alarm++;
        delay(600);
        return;
      }
    }
   if(distance1 < 20){
    delay(50);
    if(distance2 < 20){
      alarm--; 
      delay(600);
      return;
      
    }
   }

So what I am trying to do here is when Ultrasonic sensor 1(distance1)'s distance is smaller than 20 see if ultrasonic sensor 2(distance2)'s distance is smaller than 20 if true than count alarm++ and If distance2 counts less than 20 see if distance 1 is less than 20 if true than subtract alarm with 1 so alarm–.
When I run the program only the upper if statement in the program which is distance2 then distance 1 work the second one doesn’t work so that means it counts alarm++ every time even if the second condition is true which I mean when distance1 is first lesser than distance2.

Please post your actual code.

This is the Code. I am triggering sound waves from a different device and the distance I'm getting is true. Rest is the code

divyam_5129:
This is the Code.

That code doesn’t even compile.

Please post your actual code.

#define trig1 3
#define trig2 2
#define echo1 5
#define echo2 6
int alarm = 0; 
long distance1;
int duration1;
long distance2;
int duration2;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trig1, OUTPUT);
  pinMode(trig2, OUTPUT);
  
  pinMode(echo1, INPUT);
  pinMode(echo2, INPUT);
 
}

void loop() {
    digitalWrite(trig1, LOW);
  delayMicroseconds(2);
  //here we go Ultrasonic
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig1, LOW);
    duration1 = pulseIn(echo1, HIGH);
  //calculation 
  distance1 = duration1 * 0.034 / 2;
  Serial.print("distance1 = ");
  Serial.println(distance1);

  /*
   * Second input
   */
    digitalWrite(trig2, LOW);
  delayMicroseconds(2);
  //here we go Ultrasonic
  digitalWrite(trig2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig2, LOW);
  
   duration2 = pulseIn(echo2, HIGH);
   distance2 = duration2 * 0.034 / 2; 
   Serial.print("distance2 = ");
   Serial.println(distance2);
       if(distance2 < 20){
      delay(50);
      if(distance1 < 20){
        digitalWrite(led, HIGH);
        minus++; 
        delay(600);
        digitalWrite(led, LOW);
        return;
      }
    }
   if(distance1 < 20){
    delay(50);
    if(distance2 < 20){
       digitalWrite(led, HIGH);
      alarm++; 
      delay(600);
      return;
      
    }

 
}
}

Here you go!

Why nest ifs when you can do &&? Where is the - -

A very common mistake is to send ultrasound pings as fast as loop goes round.

This is far too frequent.

You should limit the total ping rate to no more than about 20 - 30 Hz, otherwise you run the risk of distant returns being mistaken for closer ones.

minus++; :smiley:

Again, that code doesn’t even compile, so please stop wasting time.

pmagowan:
Why nest ifs when you can do &&? Where is the - -

Because I want to wait until the second ultrasonic sensor condition gets true.

TheMemberFormerlyKnownAsAWOL:
A very common mistake is to send ultrasound pings as fast as loop goes round.

This is far too frequent.

You should limit the total ping rate to no more than about 20 - 30 Hz, otherwise you run the risk of distant returns being mistaken for closer ones.

 minus++;

:smiley:

That minus is supposed to be replaced by either alarm ++ or alarm--. My bad
So what should I do to make it less frequent?
Thanks

So what should I do to make it less frequent?

Slow down the loop rate.

divyam_5129:
That minus is supposed to be replaced by either alarm ++ or alarm–. My bad

But don’t you see that you’re working (or not) with one set of code, but telling us about another set?

That just wastes time.

TheMemberFormerlyKnownAsAWOL:
Slow down the loop rate.
But don’t you see that you’re working (or not) with one set of code, but telling us about another set?

That just wastes time.

So basically what I want is to count in when distance1 < 20 and wait until distance2 is less than 20cm and subtract when distance2 < 20 and wait until distance1 is less than 20cm. We have to wait because the thing will be travelling from distance1 to distance2 and vice versa.

Please Help

divyam_5129:
Please Help

We can’t see what you’ve changed or tried in the last twenty minutes.

TheMemberFormerlyKnownAsAWOL:
We can't see what you've changed or tried in the last twenty minutes.

I've tried my best, I've been doing this from a day now.

But we still can’t see it.

Take a deep breath.

Describe what “the thing” is, and how it will be “travelling”.

Describe how the results you’re getting differr from the results you expect or desire.

TheMemberFormerlyKnownAsAWOL:
But we still can’t see it.

Take a deep breath.

Describe what “the thing” is, and how it will be “travelling”.

Describe how the results you’re getting differr from the results you expect or desire.

Alright so let’s say a guy enters a store from our left side and goes to the right side, now when the person is going from left to right there will be two ultrasonic sensors which will work in a sequence like if my most left one’s distance is less because there is a person that’ll be our Ultrasonic sensor 1 and when he kinda gets far his entry will be confirmed by our second Ultrasonic sensor which will be on our right. What happening here in code is only the first if condition is working.
For eg-

//Ultrasonic sensor 1 condition 
     if(distance2 < 20){
      delay(50);
      if(distance1 < 20){
        digitalWrite(led, HIGH);
        alarm--;
        delay(600);
        digitalWrite(led, LOW);
        return;
      }
// Condition ends here 
// Ultrasonic sensor condition 2 
    }
   if(distance1 < 20){
    delay(50);
    if(distance2 < 20){
       digitalWrite(led, HIGH);
      alarm++; 
      delay(600);
      return;
      
    }
}
// condition ends here

Only Ultrasonic sensor 1 condition is working even if you make Ultrasonic sensor condition 2 true first.

I still can’t see complete up to date code.