Gather data from sonar senors while a tone be played

So what I am trying to accomplish here is to have an Uno gather data from three sonar sensors and have those values influence the time interval between each tone. Example: Object is 19 cm away from a sensor, the sound is emitted every 100ms, object is 92 cm away, sound is emitted every 1050ms.

What have you tried so far? Got any code?

Tidsuo:
So what I am trying to accomplish here is to have an Uno gather data from three sonar sensors and have those values influence the time interval between each tone. Example: Object is 19 cm away from a sensor, the sound is emitted every 100ms, object is 92 cm away, sound is emitted every 1050ms.

I think the last example code for

This is what I have so far, all it does at the moment is capture data and sort it so the highest value is in position 0 of the array.

int distances[4];

int cm1 = 0.00;
int cm2 = 0.00;
int cm3 = 0.00;

int SonarPin1 = A1;
int SonarPin2 = A2;
int SonarPin3 = A3;

int  sensorValue1;
int  sensorValue2;
int  sensorValue3;

void setup() {

  for (int x = 0; x < 3; x++) {
    distances[x] = 0;
  }

  pinMode(7, OUTPUT);
  pinMode(SonarPin1, INPUT);
  pinMode(SonarPin2, INPUT);
  pinMode(SonarPin3, INPUT);
  Serial.begin(9600);
}

void loop() {
  
  digitalWrite(7, HIGH);
  delay(20);
  digitalWrite(7, LOW);
  
  sensorValue1 = analogRead(SonarPin1);
  sensorValue2 = analogRead(SonarPin2);
  sensorValue3 = analogRead(SonarPin3);

  cm1 = (sensorValue1 * 1.26238);
  cm2 = (sensorValue2 * 1.26238);
  cm3 = (sensorValue3 * 1.26238);

  distances[0] = cm1;
  distances[1] = cm2;
  distances[2] = cm3;
  
  for(int x = 0; x < 3; x++){
  Serial.print("Sensor ");
  Serial.print(x+1);
  Serial.print(": ");
  Serial.println(distances[x]);
  }

  for (int x = 0; x < 3; x++) {
    if (distances[1] > distances[0]) {
      distances[3] = distances[0];
      distances[0] = distances[1];
      distances[1] = distances[3];
    }
    else if (distances[2] > distances[0]) {
      distances[3] = distances[0];
      distances[0] = distances[2];
      distances[2] = distances[3];
    }
  }

  delay(1000);
}

Tidsuo:

sensorValue1 = analogRead(SonarPin1);

sensorValue2 = analogRead(SonarPin2);
sensorValue3 = analogRead(SonarPin3);

Whatever type of sonar sensors that might be:
The code doesn't look like you are using common HC-SR04 ultrasonic sensors.

Would you please explain:
How would anybody be able to tell you something about your sensors, if you don't tell which type of sensors you are using?

Ah sorry yes, I am using the LV-MaxSonar-EZ0 MB1000.

Tidsuo:
Ah sorry yes, I am using the LV-MaxSonar-EZ0 MB1000.

So if you want to use more than one single sensor, this is for you:

Please scroll down until you reach the section "Sequentially Read Each MaxSonar (Always Works)".

Then follow the instructions.

I've already gotten the sensors to work together without interference, what I don't understand is how to take the values gathered and use them to produce a tone at the same time while the sensors gather another set of values.

Tidsuo:
I’ve already gotten the sensors to work together without interference, what I don’t understand is how to take the values gathered and use them to produce a tone at the same time while the sensors gather another set of values.

I’m not sure how the three distances are supposed to work together, so here is the basic idea for one distance…

Last thing in the loop function:

if(distance < someValueYouChoose){
     tone(whateverPin, whateverFrequency);
} else {
     noTone()
}

Then loop repeats and you get another distance.

Tidsuo:
I've already gotten the sensors to work together without interference, what I don't understand is how to take the values gathered and use them to produce a tone at the same time while the sensors gather another set of values.

The Arduino tone() function can use 2 or 3 parameters.

The duration of a tone is the third parameter.

So if you want to create a tone that stops automatically after some time, just use the tone() function by giving it three parameters.

Tidsuo:
I've already gotten the sensors to work together without interference, what I don't understand is how to take the values gathered and use them to produce a tone at the same time while the sensors gather another set of values.

After more tinkering that was a lie, even when wiring the RX and TX pins to each other like the diagram says they still interfere and give wrong readings every few readings. Can this be caused as to how I have the sensors mounted? They're mounted on a 30cm piece of wood one at -45 degrees, 0 degrees and +45 degrees with about 14 cm spacing from the center sensor.