Programming logic using 2 ultrasonic sensor hc-sr04

hi im trying to make a project which if sensor A detect movement LED A will light, if sensor B detect movement LED B will light up, i tri the code, but sensor A only give “out of range” output…

here is the code.

#define trigPin 2            //sensor A
#define echoPin 3          //sensor A
#define btrigPin 7          //sensor B
#define bechoPin 8         //sensor B
#define LED 13
#define LED2 12

unsigned long blinkTime = 0;

void blink(void)
{  static int x = 0;                   /* Current LED state 0=>off, 1=>on     */
   digitalWrite(LED,x ^= 1);           /* Set LED to opposite state           */
   blinkTime = millis();               /* Schedule next state change          */
}
void watch(void)
{  if (millis() - blinkTime >= 1)    /* If it's time to change state        */
      blink();                         /*  then go do it                      */
}

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(btrigPin, OUTPUT);
  pinMode(bechoPin, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;
  
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance >= 3 && distance <= 200)
  {
 
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite(LED, HIGH);
  }
  
   if (bdistance >= 3 && bdistance <= 200)
  {
 
   
    digitalWrite(LED2, HIGH);
  }
  
  else {
     Serial.println("Out of range");
     digitalWrite(LED, LOW);
     digitalWrite(LED2, LOW);
  }
  delay(50);
}

im also trying to make LED blink. is it possible to have

void blink2(void) for the other LED??

Could it be that the second sensor is getting the return echo from the first? Try a few tens of milliseconds delay between the ranging.

BTW, when you've got more than one identical sensors, driven by identical software, then it's time for a function.

will echo problem occur even the sensor are pointed at different direction??

function? im very new at programming, and i only know the basic. can you please give me an example?

will echo problem occur even the sensor are pointed at different direction??

It depends on the environment. Did you try the delay?

  int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);

I'd recommend taking a peek at the reference page. The pulseIn() function does not return an int.

i change it to this..

 int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(5000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;

no progress..

Thanks for keeping us updated.

no progress..

And pulseIn still doesn't return an int.

the led light up, but for sensor A, it give weird result

here is the code…

#define trigPin 2
#define echoPin 4
#define btrigPin 7
#define bechoPin 8
#define LED 13
#define LED2 12

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(btrigPin, OUTPUT);
  pinMode(bechoPin, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;
  

  
  if (distance >= 3 && distance <= 50)
  {
   
    Serial.print(distance);
    Serial.println(" cm");
   
    digitalWrite(LED, HIGH);
  }
  
   if (bdistance >= 3 && bdistance <= 50)
  {
 
  
    digitalWrite(LED2, HIGH);
  }
  
  else {
     Serial.println("Out of range");
     digitalWrite(LED, LOW);
     digitalWrite(LED2, LOW);
  }
  delay(500);
}

Did you give up on my idea of introducing a delay between pulses?

did you mean the delay between trigpin HIGH and trigpin LOW? i change it 5000 microsecond. still nothing happen. but right now im thinking my programming logic realy2 went wrong.

is my “if” function correct?

thank you for helping me.

No put a delay between taking the a reading and the b reading. Print raw results. Figure out what's going on.

i already put the delay function… the same thing occur. is there anything that i miss?

could you please post back the value of the 2nd sensor please...

also, voltage matters here, my breakout board came with a regulator, 3v supply was not enough and had to switch to a 5v rail, without a regulator it's important you use the correct voltage you're supposed to use.

and lastly, the sensor could be dead, swap them around see what happens (you might try that first to rule out a broken sensor/ic)

im using arduino uno, i donnt think there will be a problem with the voltage..

here is the value of 2nd sensor

Out of range
Out of range
13 cm
22 cm
11 cm
Out of range
Out of range
Out of range
13 cm
37 cm
16 cm
12 cm

the sensor is not broken, i test it one by one. it work great when alone.. but when i program it together, it went wrong..

is there anything that i miss?

Did you try printing the raw results?

Im, sorry how to print raw result? i only know how to use serial.print.

i only know how to use serial.print.

So, use that to print the values returned by the rangefinders, before you make any decisions based on those values.

instead of “out of range” post the reading … ie what’s the value…

  if (distance >= 3 && distance <= 50)
  {
   
    Serial.print(distance);
    Serial.println(" cm");
   
    digitalWrite(LED, HIGH);
  }
  
   if (bdistance >= 3 && bdistance <= 50)
  {
 
  
    digitalWrite(LED2, HIGH);
  }
  
  else {
     Serial.println("Out of range");
     Serial.print(bdistance);
     Serial.println(" cm");

     digitalWrite(LED, LOW);
     digitalWrite(LED2, LOW);
  }
  delay(500);

i use "else if" right now and i use the delay. it seems to work fine.. thank you for all your help guys.. :D

Could you list your current code that works? I just purchased one of these sensors on ebay and need some help figuring out the code for it. I thought this module would be the same as the Ping))). I was wrong, but hope to get it to work just as well.

Thank you.