Sound installation with two HC-SR04 sensors: help needed
Hey guys;
I'm trying to make a sound installation that takes data from two separate HC-SR04 sensors, and puts each stream of data into a separate number box. From there, I can add other parameters. However, I don't know how to separate the data streams. When I turn one sensor on, it works normally. When I turn the other sensor on, the data from the one that was on first just overrides it and I get two identical streams of data. How can I separate the two sensors? I attached a screenshot of my max patch and Arduino code.

#define echoPin 11 // The number of the digital pin connected to the echo input
#define trigPin 12 // The number of the digital pin connected to the trigger output
#define LEDPin 13 // This enables the LED feedback for testing
#define echoPin 8 // The number of the digital pin connected to the echo input
#define trigPin 9 // The number of the digital pin connected to the trigger output
#define LEDPin 10 // This enables the LED feedback for testing
int maximumRange = 60; // The max distance observed from the sensor
int minimumRange = 0;
float duration, distance; //duration used to calculate distance
void setup() {
Serial.begin(9600); //starts the serial communication via USB
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); //board LED for testing
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // pulse off
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // pulse for 10 microseconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // echo pin listens / receives
// from that data calculate distance in CentiMeters based on Speed of Sound
distance = duration / 58.2;
// error handling
if (distance >= maximumRange || distance <= minimumRange) {
Serial.println("-1"); //if nothing observed ouput -1
digitalWrite(LEDPin, HIGH); // LED will be on
} else {
Serial.println(distance); // prints the distance in CM
digitalWrite(LEDPin, LOW); // LED will be off if object detected
}
delay(50); // good practice not to overload the serial port
}
Hi, you need to define the readings of each sensors something like this:
===========
#define echoPin 5
#define trigPin 6
#define echoPin01 7
#define trigPin01 8
int distance = 0; int distance01 = 0;
long duration; long duration01;
int val[6] = {0, 0, 0, 0, 0, 0,};
void setup() { Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin01, OUTPUT);
pinMode(echoPin01, INPUT); } void loop() {
for(int i = 0; i < 6; i++) {val[i] = analogRead(i);
Serial.print(val[i]);Serial.print(" ");}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Serial.print(distance); Serial.print(" ");
digitalWrite(trigPin01, LOW);
delayMicroseconds(2);
digitalWrite(trigPin01, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin01, LOW);
duration01 = pulseIn(echoPin01, HIGH);
distance = duration * 0.034 / 2;
distance01 = duration01 * 0.034 / 2;
Serial.println(distance01); delay(50);
}
================
WARNING, this is an old script, I remember that it was running ok, but I see something strange in the loop (an AnalogRead and don' remember what is suppose todo).
Anyway give a look here with the New ping Library:
You could start with the Ping 3 Sensors Sketch
DO NOT USE THIS :
for(int i = 0; i < 6; i++) {val[i] = analogRead(i);
Serial.print(val[i]);Serial.print(" ");}
it will flood serial port for nothing !
..........
HCSR04 library is easier to use , even with multiple devices
only 1 trigger pin is needed for all sensors,
only echo pins need to be separated
......
That original posted code has pins assigned, but no readings at all for 2nd sensor.
you should not use 2 serial objects to receive 2 sensors
P.S. you might want to read this thread :
Hi Kreiner, did you then solve it with two hcsr04s? I also needed to use two sensors with an Arduino. You can post the sketch.ino and the max patch. A thousand thanks