Daisy chain 2 Ultrasonic sensors.

Hi Guys,

I would like to daisy chain 2 maxbotix XL ultrasonic sensors in such a way that i get 5 distance readings from the first sensor and 1 from the other sensor. I can read 1 value at a time from both sensors but dont know how to change it to get 5 from the first sensor and only one reading from the other. Moreover, I am also using a potentiometer to control the movement of a ESC motor(which works fine). I am totally new to this. Please help!

Here is the sample code.

#include <Servo.h> 

int signalPinOne = 3;
int signalPinTwo = 5;
int triggerPin = 12;   

Servo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 

void setup() { 
  Serial.begin(9600); 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
    pinMode(triggerPin, OUTPUT); // set this pin to output 
  //give the sensors time to boot up 
  delay(250); 
} 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
   myservo.write(val);                  // sets the servo position according to the scaled value 
  digitalWrite(triggerPin, HIGH); 
  delayMicroseconds(30); 
  digitalWrite(triggerPin, LOW); 
  delay(50); 

   //Listen for echo
  int valueTwo = analogRead(signalPinTwo); 

  
  Serial.println();
  //Serial.print("UltraSound Value ONE");
  //Serial.print(  "= ");
  
  int valueOne = analogRead(signalPinOne);
  Serial.print(valueOne);
  Serial.println();
  
  //Serial.println();
  //Serial.print("UltraSound Value TWO");
  //Serial.print(  "= ");
  Serial.print(valueTwo);
 // Serial.println();
  
  //Serial.println();

  delay(180); 

 // Serial.println();
  //Serial.println();  

}

A for loop is what you need - see the reference on the main site for details.

What are you planning to do with these results? If you want to keep them all, arrays will help, or perhaps you plan to sum them to calculate an average?

Thank your for your reply.

I plan to see the influence of the propeller movement on the sensor values if there is any difference. I do not want to average them. I only want to see if there are any fluctuations in them when i move my Esc motor. So what i thought of is that i put the function trigger() five times in a loop and read the first sensor and once for the second one. I dont know if this Is a good solution....what do you think?

void triger()
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(30);
digitalWrite(triggerPin, LOW);
delay(50);
}

moreover, I am keeping these values in a csv file using processing.

It might be easier if your trigger function took a parameter specifying the analog pin you want to sample, performed the reading and returned it from trigger. That way you can call it once for your second value and five times in a loop for the first one.