help in combining multiple results of ultrasonic sensors

hi
i changed the PING function to output 1 if the distance to the target is more than 1.5 m else 0
so i wanted to combine the result of a five sensors in a five digits numbers so i can determine the position of the obstacle so i used this so i used this code but it still doesn’t generate the required result

go(); // if nothing is wrong the go forward using go() function below.
int j=1;
for(int i = 0; i < 5; i++) //Obstacle position detection
{
pingPin = i;
int distance = ping(); // us the ping() function to see if anything is ahead.
result+ = (distancej); //Sumition of the sensors output
j=j
10;
}

can anyone tell me why?

^^^^up^^^^

Where is the rest of your code? What is the purpose of assigning a value to pingPin? Why isn't the pin to be used passed to the ping() function?

If the ping() function returns a flag, why is the flag stored in a variable called distance? Why isn't the variable called something like thePriceOfBananasInHonolulu or something meaningful?

What is the required result? What is the actual result?

This is just about the least useful post all week.

And quit bumping threads after just over an hour!

here is the whole code it’s supposed to move away the ship by rotating the servo that is connected to the helm
the question mark isn’t in the uploaded code I am waiting to determine the angle by the trail

 #include <Servo.h>
//Declare Servo
Servo myservo;     //Ping Sensor Servo
const int turntime = 500;  //Number of milliseconds to turn when turning
int turndegree = 90;		//Degree of the servo motor to turn when turning
const int pingPin;     //Pin that the Ping sensor is attached to.
const int myservopin = 9;   // Pin number for helm servo
const int distancelimit = 150;   //If something gets this many inched from
                                 // the robot it stops and looks for where to go.
//Setup function. Runs once when Arduino is turned on or restarted
void setup()
{
	myservo.attach(myservopin); // Attach the scan servo
	delay(2000);        // wait two seconds
}
void loop()
{
	go();  // if nothing is wrong the go forward using go() function below.
	int j=1;
	for(int i = 0; i < 5; i++)		//Obstacle position detection 
	{
		pingPin = i;
		int distance = ping(); // us the ping() function to see if anything is ahead.
		result+ = (distance*j);		//Sumition of the sensors output
		j=j*10;
	}	

	if (distance < distancelimit){
		stopmotors(); // If something is ahead, stop the motors.
		char turndirection = scan(); //Decide which direction to turn.
		switch (turndirection)
		{
		case 'l':
			turnleft(turntime);
			break;
		case 'r':
			turnright(turntime);
			break;
		case 's':
			turnleft(turnaroundtime);
			break;
		}
	}
}
 
int ping()
{
	long duration, inches, cm;
	//Send Pulse
	pinMode(pingPin, OUTPUT);
	digitalWrite(pingPin, LOW);
	delayMicroseconds(2);
	digitalWrite(pingPin, HIGH);
	delayMicroseconds(5);
	digitalWrite(pingPin, LOW);
	//Read Echo
	pinMode(pingPin, INPUT);
	duration = pulseIn(pingPin, HIGH);
	// convert the time into a distance
	inches = microsecondsToInches(duration);
	cm = microsecondsToCentimeters(duration);
	Serial.print("Ping:  ");
	Serial.println(cm);
	if(cm>=50)
		return 1;
	else
		return 0;
	//return round(cm);
}
void go()
{
	myservo.write(90);
}
void turnleft(int t,int deg)
{
	deg = 90 - deg ;
	myservo.write(deg);
	delay(t);
}   
void turnright(int t,int deg)
{
	deg = 90 + deg ;
	myservo.write(deg);
	delay(t);
}      
void forward(int t)
{
	myservo.write(90);
	delay(t);
}
void stopmotors()
{
	myservo.write(90);
}
void obistacleAvoidance(result)
{
	if(result==00000||result==10001)
		stopmotors();
	else if (result==00100)
		turnleft(turntime,20);
	else if (result==00001)
		turnleft(turntime,15);
	else if (result==00010||result==00011)
		turnleft(turntime,30);
	else if (result==00101||result==00110||result==00111)
		turnleft(turntime,45);
	else if (result==01100||result==10100||result==11100)
		turnleft(turntime,45);
	else if (result==01000||result==11000)
		turnleft(turntime,30);
	else if (result==10000)
		turnright(turntime,15);
	else if (result==01001)
		????????(turntime,??);
	else if (result==01010)
		????????(turntime,??);
	else if (result==01011)
		????????(turntime,??);
	else if (result==01101)
		????????(turntime,??);
	else if (result==01110)
		????????(turntime,??);
	else if (result==01111)
		????????(turntime,??);
	else if (result==10010)
		????????(turntime,??); 
	else if (result==10011)
		????????(turntime,??); 
	else if (result==10101)
		????????(turntime,??); 
	else if (result==10110)
		????????(turntime,??); 
	else if (result==10111)
		????????(turntime,??); 
	else if (result==11001)
		????????(turntime,??); 
	else if (result==11010)
		????????(turntime,??); 
	else if (result==11101)
		????????(turntime,??); 
	else if (result==11110)
		????????(turntime,??); 
	else if (result==11111)
		????????(turntime,??);
}
char scan()
{
	int leftscanval, centerscanval, rightscanval;
	char choice;
	//Look left
	myservo.write(30);
	delay(300);
	leftscanval = ping();
	//Look right
	myservo.write(150);
	delay(1000);
	rightscanval = ping();
	//center scan servo
	myservo.write(88);

	if (leftscanval>rightscanval && leftscanval>centerscanval)
	{
		choice = 'l';
	}
	else if (rightscanval>leftscanval && rightscanval>centerscanval)
	{
		choice = 'r';
	}
	else
	{
		choice = 's';
	}
	Serial.print("Choice:  ");
	Serial.println(choice);
	return choice;
}
long microsecondsToInches(long microseconds)
{
	return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
	return microseconds / 29 / 2;
}
const int pingPin;     //Pin that the Ping sensor is attached to.

A const variable car be assigned a value ONLY when it is defined. You don’t, so it gets a default value of 0. It can then never be changed.

		pingPin = i;

That won’t even compile. What is the point of posting code that won’t even compile?

	go();  // if nothing is wrong the go forward using go() function below.

If nothing is wrong? the if statement seems to be missing.

		int distance = ping(); // us the ping() function to see if anything is ahead.
		result+ = (distance*j);		//Sumition of the sensors output
		j=j*10;
	}	

	if (distance < distancelimit){

distance went out of scope when the for loop ended. You can’t use distance here. That won’t even compile. What is the point of posting code that won’t even compile?

	else if (result==00100)

00100 is an octal constant, equal to 64. It’s unlikely that result will ever be 64.

	else if (result==00010||result==00011)

Or 8 or 9.

Get rid of the leading 0s.

PaulS:

const int pingPin;     //Pin that the Ping sensor is attached to.

A const variable car be assigned a value ONLY when it is defined. You don’t, so it gets a default value of 0. It can then never be changed.

i have removed the const

PaulS:

		pingPin = i;

That won’t even compile. What is the point of posting code that won’t even compile?

i have five ultrasonic sensors so i put this statement to read the reading of all of them and rotate the servo depending on the final result of all of them

PaulS:

	go();  // if nothing is wrong the go forward using go() function below.

If nothing is wrong? the if statement seems to be missing.

removed it

PaulS:

		int distance = ping(); // us the ping() function to see if anything is ahead.
	result+ = (distance*j);		//Sumition of the sensors output
	j=j*10;
}	

if (distance < distancelimit){


distance went out of scope when the for loop ended. You can't use distance here. That won't even compile. What is the point of posting code that won't even compile?
deleted it and put this code in the loop()


void loop()
{
int result;
go();  // if nothing is wrong the go forward using go() function below.
int j=1;
for(int i = 0; i < 5; i++) //Obstacle position detection
{
pingPin = i;
int distance = ping(); // us the ping() function to see if anything is ahead.
result+ = (distancej); //Sumition of the sensors output
j=j
10;
}
obistacleAvoidance(result);
}

PaulS:

	else if (result==00100)

00100 is an octal constant, equal to 64. It’s unlikely that result will ever be 64.

	else if (result==00010||result==00011)

Or 8 or 9.

Get rid of the leading 0s.

done

void loop()
{
	int result;
	go();  // if nothing is wrong the go forward using go() function below.
	int j=1;
	for(int i = 0; i < 5; i++)		//Obstacle position detection 
	{
		pingPin = i;
		int distance = ping(); // us the ping() function to see if anything is ahead.
		result+ = (distance*j);		//Sumition of the sensors output
		j=j*10;
	}	
	obistacleAvoidance(result);
}

You still need an if statement to decide whether or not to go().
result will always be set to 1 or 0 (00 or 10 or 1*1 are the only operations.

You have a few problems in assigning a value to j.
You get a value of 0 or 1 from the ping() routine, so you should not be initializing j with a 1.
The proper order to do things to get your desired result is:

Set J to 0 before the for
before each ping(), multiply j *10
make j = j + ping()

This will give you a 5 digit number, but then in order to do anything with the individual readings, you need to mess about with math to extract them. You’d be better off using an array of bytes, ints, booleans, or whatever, and if you need an indication that at least one of the sensors detects something, a separate boolean could be set to true when any ping() returns a 1.

i tried to change my program so i started with 2 sensors i put one the left and one on the right, so if the left object is nearer than the right one it output "L", & if the right object is nearer than the left one it output "R", else it output "C", but all i get is "C" what is the problem?

#define trigPin 1
#define echoPin1 2
#define echoPin2 3
void setup(){
  Serial.begin (9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin1,INPUT);
  delay(500);
  pinMode(echoPin2,INPUT);
}
void loop(){
  char c;
  c=scan();
  Serial.println(c);
  delay(500);
}
char scan()
{
  int left, right;
  char choice;
  left = png('l');
  right = png('r');
  if (left>right){
    choice='L';
  }
  else if (right>left){
    choice='R';
  }
  else{
    choice='C';
  }
  return choice;
}
int png(char c)
{
  int duration,distance;
  if (c='l'){
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin,LOW);
    duration = pulseIn(echoPin1,HIGH);
    distance = (duration/2) / 29.1;
  }
  if (c='r'){
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin,LOW);
    duration = pulseIn(echoPin2,HIGH);
    distance = (duration/2) / 29.1;
  }
  return distance;
}

lar3ry:

void loop()

{
int result;
go();  // if nothing is wrong the go forward using go() function below.
int j=1;
for(int i = 0; i < 5; i++) //Obstacle position detection
{
pingPin = i;
int distance = ping(); // us the ping() function to see if anything is ahead.
result+ = (distancej); //Sumition of the sensors output
j=j
10;
}
obistacleAvoidance(result);
}




You still need an **if** statement to decide whether or not to go().
result will always be set to 1 or 0 (0*0 or 1*0 or 1*1 are the only operations.

You have a few problems in assigning a value to j.
You get a value of 0 or 1 from the ping() routine, so you should not be initializing j with a 1.
The proper order to do things to get your desired result is:

Set J to 0 before the **for**
before each ping(), multiply j *10
make j = j + ping()

This will give you a 5 digit number, but then in order to do anything with the individual readings, you need to mess about with math to extract them. You'd be better off using an array of bytes, ints, booleans, or whatever, and if you need an indication that at least one of the sensors detects something, a separate boolean could be set to true when any ping() returns a 1.

i removed the go() function and did like you said but still no result

i removed the go() function and did like you said but still no result

If you want to call go() every time through loop(), fine, but remove the comment! There's no IF involved.

I am having a tough time figuring out what you mean by "still no result". Do you mean the result variable? If so, what do you mean by "no result". If not, tell us what result you want..

And show us what your current loop() code looks like. We aren't mind readers!

i tried to change my program so i started with 2 sensors i put one the left and one on the right, so if the left object is nearer than the right one it output "L", & if the right object is nearer than the left one it output "R", else it output "C", but all i get is "C" what is the problem?

Well, for starters, you are triggering the same pin for both left and right. You aren't triggering your right sensor.

lar3ry:

i removed the go() function and did like you said but still no result

If you want to call go() every time through loop(), fine, but remove the comment! There’s no IF involved.

I am having a tough time figuring out what you mean by “still no result”. Do you mean the result variable? If so, what do you mean by “no result”. If not, tell us what result you want…

And show us what your current loop() code looks like. We aren’t mind readers!

the servo isn’t moving at all it’s still on its initial state

the loop()

void loop()
{
	int j;
	j=0;
	for(int i = 1; i < 3; i++)		//Obstacle position detection 
	{
		pingPin = i;
		j = j*10;
		int distance = ping(); // us the ping() function to see if anything is ahead.
		j=j+ping();
	}
    obistacleAvoidance(j);
}

lar3ry:

i tried to change my program so i started with 2 sensors i put one the left and one on the right, so if the left object is nearer than the right one it output “L”, & if the right object is nearer than the left one it output “R”, else it output “C”, but all i get is “C”
what is the problem?

Well, for starters, you are triggering the same pin for both left and right. You aren’t triggering your right sensor.

so here i need to use a different pin for trig, right?

mohd9011:
the servo isn’t moving at all it’s still on its initial state

This is the first thing you’ve said about servos. You’ve been asking about multiple ultrasonic sensors.

the loop()

void loop()

{
int j;
j=0;
for(int i = 1; i < 3; i++) //Obstacle position detection
{
pingPin = i;
j = j*10;
int distance = ping(); // us the ping() function to see if anything is ahead.
j=j+ping();
}
   obistacleAvoidance(j);
}




so here i need to use a different pin for trig, right?

Not in the code quoted here… I was commenting on the code you posted saying you had two sensors, and the ping routines kept returning ‘C’. In that code, you defined the left ping pin, and used it in the left and right pings.

So, please post your entire current code, with all the changes you’ve made. It’s impossible to visualize what it looks like now without seeing it.

I've been meaning to make some troubleshooting suggestions.

  1. Try to get the sensors working first, without worrying about the servos. All programming should be done this way, dealing with one thing at a time.

  2. Place some Serial.print() and Serial.println() statements in your ping() function, right after the pulsein statement. This will let you see exactly how far the sensor thinks the obstacle is (in raw, round-trip time). Once you get that working, place prints after the calculations, then after the call to ping(), to make sure the whole ping function is working. Print the pin number as well, so you can see which sensors are giving you problems, if any. Every time you find out what your code produces, you can compare it with what you think it SHOULD produce, and you can take steps to correct it.

Write, test, fix, then repeat as necessary. It will eventually yield a program that works.

i wrote this as a test to see the output of each (4pin) sensor

void setup(){
  Serial.begin (9600);
}
void loop(){
  int distance1,distance2;
  distance1 = Ping(2,3);
  Serial.print(distance1);
  Serial.println(" d1 cm");
  distance2 = Ping(2,4);
  Serial.print(distance2);
  Serial.println(" d2 cm");
  delay(500);
}
int Ping(int trigPin,int echoPin)
{
  int duration, distance;
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  distance = (duration/2) / 29.1;
  if(distance>250 || distance<0)distance=-250;
  return distance;
}

when i cover the second sensor - distance2 = Ping(2,4); - the first reading is perfect, but when i cover the first one - distance1 = Ping(2,3); - it affects the second result , and some times it just output strange outputs

1- the normal output

2- covering second sensor

3- covering first sensor

4- the strange outputs

what can i do to solve this?

what can i do to solve this?

Pretty simple, really. Don't use the same trigger pin for both sensors. You are triggering both sensors to send pulses at the same time.

PaulS:

what can i do to solve this?

Pretty simple, really. Don’t use the same trigger pin for both sensors. You are triggering both sensors to send pulses at the same time.

isn’t there any other solution because iam going to use 4 ultrasonic sensors, 3 channel relay and gps on arduino uno will the pins will be enough?

and when i try seprating the trig pins

void setup(){
  Serial.begin (9600);
}
void loop(){
  int distance1,distance2;
  distance1 = Ping(1,2);
  distance2 = Ping(3,4);
  Serial.print(distance1);
  Serial.println(" d1 cm");
  Serial.print(distance2);
  Serial.println(" d2 cm");
  delay(500);
}
int Ping(int trigPin,int echoPin)
{
  int duration, distance;
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  distance = (duration/2) / 29.1;
  if(distance>250 || distance<0)distance=-250;
  return distance;
}

this come out

and i have another problem when i connect the ground in the ardunio uno chip it just shutdown what wrong did i do?

  Serial.begin (9600);
}
void loop(){
  int distance1,distance2;
  distance1 = Ping(1,2);

Perhaps you just need to put the Arduino away. The use of pins IS documented. You can NOT use pin 1 and Serial at the same time.