five ultrasonic hc-sr04

Hi - still chaos in my sketch -

i installed 5 ultrasonic HC-SR04 parallel on an 1,60 m line - turned it in a cirkle against interferences…new ping shows still wrong distances…and it doesn`t switch the ventilator…

thanks for sharing your successfull experiment or your wisdom !

#include <NewPing.h>

int trigPin1=2;

int echoPin1=3;

int trigPin2=4;

int echoPin2=5;

int trigPin3=6;

int echoPin3=7;

int trigPin4=8;

int echoPin4=9;

int trigPin5=10;

int echoPin5=11;

int buzzPin = 12;

void setup() {

Serial.begin (9600);

pinMode(trigPin1, OUTPUT);

pinMode(echoPin1, INPUT);

pinMode(trigPin2, OUTPUT);

pinMode(echoPin2, INPUT);

pinMode(trigPin3, OUTPUT);

pinMode(echoPin3, INPUT);

pinMode(trigPin4, OUTPUT);

pinMode(echoPin4, INPUT);

pinMode(trigPin5, OUTPUT);

pinMode(echoPin5, INPUT);
pinMode (buzzPin, OUTPUT);

}

void loop() {

long duration1, distance1;

digitalWrite(trigPin1, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin1, HIGH);

delayMicroseconds(10); // Added this line

digitalWrite(trigPin1, LOW);

duration1 = pulseIn(echoPin1, HIGH);

distance1 = (duration1/2) / 29.1;

if (distance1 >= 150 || distance1 <= 0){

Serial.print("Sensor1 ");

Serial.print(distance1);

Serial.println(“cm”);
digitalWrite (buzzPin, LOW);
}

else {

Serial.print ( "Sensor1 ");

Serial.print ( distance1);

Serial.println(“cm”);

digitalWrite (buzzPin, HIGH);

}

delay(1500);

long duration2, distance2;

digitalWrite(trigPin2, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin2, HIGH);

delayMicroseconds(10); // Added this line

digitalWrite(trigPin2, LOW);

duration2 = pulseIn(echoPin2, HIGH);

distance2= (duration2/2) / 29.1;

if (distance2 >= 150 || distance2 <= 0){

Serial.print("Sensor2 ");

Serial.print(distance2);

Serial.println(“cm”);

digitalWrite (buzzPin, LOW);
}

else {
digitalWrite (buzzPin, HIGH);
Serial.print("Sensor2 ");

Serial.print(distance2);

Serial.println(“cm”);

}

delay(1500);

long duration3, distance3;

digitalWrite(trigPin3, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin3, HIGH);

delayMicroseconds(10); // Added this line

digitalWrite(trigPin3, LOW);

duration3 = pulseIn(echoPin3, HIGH);

distance3= (duration3/2) / 29.1;

if (distance3 >= 150 || distance3 <= 0){

Serial.print("Sensor3 ");

Serial.print(distance3);

Serial.println(“cm”);

digitalWrite (buzzPin, LOW);
}

else {
digitalWrite (buzzPin, HIGH);
Serial.print("Sensor3 ");

Serial.print(distance3);

Serial.println(“cm”);

}

delay(1500);

long duration4, distance4;

digitalWrite(trigPin4, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin4, HIGH);

delayMicroseconds(10); // Added this line

digitalWrite(trigPin4, LOW);

duration4 = pulseIn(echoPin4, HIGH);

distance4= (duration4/2) / 29.1;

if (distance4 >= 150 || distance4 <= 0){

Serial.print("Sensor4 ");

Serial.print(distance4);

Serial.println(“cm”);
digitalWrite (buzzPin, LOW);
}

else {
digitalWrite (buzzPin, HIGH);
Serial.print("Sensor4 ");

Serial.print(distance4);

Serial.print(“cm”);

}

delay(1500);

long duration5, distance5;

digitalWrite(trigPin5, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin5, HIGH);

delayMicroseconds(10); // Added this line

digitalWrite(trigPin5, LOW);

duration5 = pulseIn(echoPin5, HIGH);

distance5= (duration5/2) / 29.1;

if (distance5 >= 350 || distance5 <= 0){

Serial.print("Sensor5 ");

Serial.print(distance5);

Serial.println(“cm”);

digitalWrite(buzzPin, LOW);
}

else {
digitalWrite (buzzPin, HIGH);
Serial.print(“Sensor5”);

Serial.print(distance5);

Serial.println(“cm”);

}

delay(2000);
}

That sketch can be reduced in length by about 80% by using arrays (one for the echo pins, one for the trig pins, and then a for loop to read them all). In the process you reduce the compiled size a lot, and make the whole thing much easier to read and maintain.

Also do read the sticky, so you know how to properly post code and what other info you missed (such as the actual reading, photo of the setup, circuit diagrams, etc).