How to programming multiple HC-SR04 sensor

Hello People,

I've a arduino project with 4 HC-SR04 sensor and 4 led. I just can turning on the first sensor, my problem is in program turning on the fourth sensor in the same time. If anyone can help, I'm very grateful. Below is a program with one sensor successfully.

#define trigPin_1 6
#define echoPin_1 2
#define led_1 10
#define trigPin_2 7
#define echoPin_2 3
#define led_2 11


void setup() {
  Serial.begin (9600);
  pinMode(trigPin_1, OUTPUT);
  pinMode(echoPin_1, INPUT);
  pinMode(led_1, OUTPUT);
  pinMode(trigPin_2, OUTPUT);
  pinMode(echoPin_2, INPUT);
  pinMode(led_2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin_1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin_1, LOW);
  duration = pulseIn(echoPin_1, HIGH);
  distance = (duration/2) / 29.1;
  
  

  if (distance <= 30 ) {
    digitalWrite(led_1, LOW);
    
}

  else {
    digitalWrite(led_1, HIGH);
    
    delay(500);
  }
  

  delay(10);

}

my problem is in program turning on the fourth sensor in the same time

What problem are you having ?

Problem is I don't know to program(skecth) to turn on the fourth of sensor and led in the same time. I've tried but still can't. Can You help?

The simple but clumsy way would be to repeat the code 4 times.
A smarter way would be to write a function to do the readings and turn on the LEDs. Using arrays would be even smarter.

Handa:
I've a arduino project with 4 HC-SR04 sensor and 4 led. I just can turning on the first sensor, my problem is in program turning on the fourth sensor in the same time. If anyone can help, I'm very grateful. Below is a program with one sensor successfully.

Did you ever hear of something like 'structured programming', 'array' and 'for-loop'?

Here is a program that is putting all the pin numbers into arrays and asking each sensor in a for-loop for its distance (untested code, except tested for error-free compilation):

byte trigPins[]={6,7,8,9};
byte echoPins[]={2,3,4,5};
byte ledPins[]={10,11,12,13};
#define SENSORTIMEOUT 30000 // in microseconds
#define NUMSENSORS (sizeof(echoPins))

void setup() {
  Serial.begin (9600);
  for (int i=0; i<NUMSENSORS; i++)
  {
    pinMode(trigPins[i], OUTPUT);
    pinMode(echoPins[i], INPUT);
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  long duration, distance;
  for (int i=0; i<NUMSENSORS; i++)
  {
    digitalWrite(trigPins[i], LOW);
    delayMicroseconds(2);
    digitalWrite(trigPins[i], HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPins[i], LOW);
    duration = pulseIn(echoPins[i], HIGH, SENSORTIMEOUT); // don't wait too long for an echo
    if (duration==0) duration=SENSORTIMEOUT;
    distance = (duration/2) / 29.1;
    if (distance <= 30 )
      digitalWrite(ledPins[i], LOW);
    else
      digitalWrite(ledPins[i], HIGH);
  }
  delay(2*(SENSORTIMEOUT/1000)); // wait long enough for all echos to vanish
}

I have also limited the waiting time for the "pulseIn()" function to 30000µs = 30 milliseconds, so that those sensors that are out of the echo range will not slow down the application.

This code should run more than 10 times per second through the loop, so each led should be switched in less than 0.4 seconds after the distance has changed beyond the limit of 30 centimeters.

Please try to understand arrays and for-loops!

I have also limited the waiting time for the "pulseIn()" function to 30000µs = 30 milliseconds, so that those sensors that are out of the echo range will not slow down the application.

The NewPing library uses interrupts, to prevent these kinds of problems.

jurs:
Please try to understand arrays and for-loops!

Thanks for the code jurs. this worked. I'll try to understand arrays and for-loops!!