Uno + Ultrasonic Sensor

Guys help me. Please help me with the code. How can i program a series of sensors when only the sensor that sense the nearer object can send ping to the arduino. Thanks guys. Sorry for the grammar. Help me.

Where is your code? Can you draw a picture of your sensors and objects by hand and post a photo of the drawing?

liudr:
Where is your code? Can you draw a picture of your sensors and objects by hand and post a photo of the drawing?

This is the code but all the sensor can send ping to the arduino, We dont know how to program it. The sensor and objects is on my group mates so i cant send the picture. If you can help me with the program i will try it immediately. Thank you for the response

/*
CODE HERE
Array of HC-SR04 ultrasonic sensors
*/

//Sonar 1
int echoPin1 =2;
int initPin1 =3;
int distance1 =0;

//Sonar 2
int echoPin2 =6;
int initPin2 =7;
int distance2 =0;

//Sonar 3
int echoPin3 =8;
int initPin3 =9;
int distance3 =0;

//Sonar 4
int echoPin4 =12;
int initPin4 =13;
int distance4 =0;

void setup() {

pinMode(initPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(initPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(initPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(initPin4, OUTPUT);
pinMode(echoPin4, INPUT);
Serial.begin(9600);

}

void loop() {

distance1 = getDistance(initPin1, echoPin1);
printDistance(1, distance1);
delay(150);

distance2 = getDistance(initPin2, echoPin2);
printDistance(2, distance2);
delay(150);

distance3 = getDistance(initPin3, echoPin3);
printDistance(3, distance3);
delay(150);

distance4 = getDistance(initPin4, echoPin4);
printDistance(4, distance4);

delay(500);

}

int getDistance (int initPin, int echoPin){

digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);
unsigned long pulseTime = pulseIn(echoPin, HIGH);
int distance = pulseTime/58;
return distance;

}

void printDistance(int id, int dist){

Serial.print(id);
if (dist >= 120 || dist <= 0 ){
Serial.println(" Out of range");
}else
for (int i = 0; i <= dist; i++) {
Serial.print("-");
}
Serial.print(dist, DEC);
Serial.println(" cm");

}

This code seems to be working. If you want to find which one is the shortest distance, you compare the four distances and only print the lowest number. Is that what you want?

Yes that what i want. We try comparing it and it so slow to compare. We need fast comparing like 10ms. Because after that the ping will be the input for our playback memory and notify the user through messages.

liudr: This code seems to be working. If you want to find which one is the shortest distance, you compare the four distances and only print the lowest number. Is that what you want?

We try comparing in normal code. But it so slow. Some said that we try comparing in array but we dont know how. Can you help me with the code?

At the moment you have delays between pings. You are already running your print function between pings. Are u doing a compare between pings? i.e. are u comparing each new reading with the last nearest reading - if not, u should. Consider printing out 2 readings: 1. Each sensor reading (as u are doing now) 2. The last nearest reading + sensor id. I would speculate you could then also reduce the delay.

aisc: At the moment you have delays between pings. You are already running your print function between pings. Are u doing a compare between pings? i.e. are u comparing each new reading with the last nearest reading - if not, u should. Consider printing out 2 readings: 1. Each sensor reading (as u are doing now) 2. The last nearest reading + sensor id. I would speculate you could then also reduce the delay.

what should i do?

Don't cross-post!

I already outlined what you could do...

Introduce another variable "distance_to_nearest_object" and initialise it at anything above 120.

Compare each sensor's distance reading to the current value of "distance_to_nearest_object" and if the reading is lower, make "distance_to nearest_object" equal to the lower reading.

If you cycle through all four sensors and the distances are all greater than "distance_to_nearest_object", then u have an invalid "distance_to_nearest_object" - you will have to figure out how you want to handle that.

Add a print command to you function to print "distance_to_nearest_object" as well.

aisc: Don't cross-post!

I've already deleted the duplicate