How to get Arduino to take an action based upon the term it is returned

I was hoping someone would be able to help me out with this, i'm sure it's quite simple however i'm new to this and don't know how to do it. My Arduino is connected to 3 sensors and the Arduino performs a mathematical calculation with the data it gets from the sensors and then works out which is the largest number using this piece of code:

`````` "sensor", "sensor2", "sensor3";

printf("Enter three numbers: ");
scanf("%lf %lf %lf", &sensor, &sensor2, &sensor3);

if( sensor>=sensor2 && sensor>=sensor3 )
printf("%.2f is the largest number.", sensor);

if( sensor2>=sensor && sensor2>=sensor3 )
printf("%.2f is the largest number.", sensor2);

if( sensor3>=sensor && sensor3>=sensor2 )
printf("%.2f is the largest number.", sensor3);

return ;
``````

After it returns me the largest number I need to set a servo to a position based upon the sensor that gave the largest number. Say, for example that Sensor2 gave me the highest value then the servo would need to be set to position 2 , and if Sensor3 gave me the highest value then the servo would need to be set to position 3.

If anybody would be able to help me with this I would appreciate it, I am preparing myself to facepalm at the simplicity of this!

It would make a lot more sense if the sensors were numbered something other than yeah, 2, 3.

It would make a lot more sense if you posted ALL of your code.

I am preparing myself to facepalm at the simplicity of this!

Good, because it really is simple. You've already determined the largest value, so you simply need a collections of positions, and apply the appropriate one when you print the useless message. Maybe call them positionYeah, position2, and position3. Or get smart and start using arrays.

As @PaulS has said, please post a complete program.

To be honest it is not clear from your Original Post what is the problem as you seem to have the more complicated part figured out.

Maybe you should have another variable that holds the number of the sensor with the largest value? Something like

``````byte sensorWithLargestVal = 1;
if (sensor2val > sensor1val) {
sensorWithLargestVal = 2;
}
// etc
``````

...R

Okay so here is the complete program:

``````const int trigPin = 9;
const int echoPin = 10;
const int trigPin2 = 11;
const int echoPin2 = 12;
const int trigPin3 = 7;
const int echoPin3 = 8;

long duration, cm, inches;
int distance;
int duration2;
int distance2;
int duration3;
int distance3;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);

digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
digitalWrite(trigPin3, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2*0.034/2;
duration3 = pulseIn(echoPin3, HIGH);
distance3 = duration3*0.034/2;

Serial.print("distance: ");
Serial.println(distance);
Serial.print("distance2: ");
Serial.println(distance);
Serial.print("distance3: ");
Serial.println(distance);

"distance", "distance2", "distance3";

printf("Enter three numbers: ");
scanf("%lf %lf %lf", &distance, &distance2, &distance3);

if( distance>=distance2 && distance>=distance3 )
printf("%.2f is the largest number.", distance);

if( distance2>=distance && distance2>=distance3 )
printf("%.2f is the largest number.", distance2);

if( distance3>=distance && distance3>=distance2 )
printf("%.2f is the largest number.", distance3);

return ;
}
``````

As you might be able to tell it is using ultrasonic distance sensors (HC-SR04's) to take distance measurements.

The aim of the program is to take distance readings from 3 HC-SRO4's (all mounted on the front of what will hopefully become a slightly autonomous robot, there will be a central sensor that reads directly in front and the other 2 sensors will be slightly offset pointing to the left and the right). The program then chooses the sensor that gives off the highest reading and sets a servo (which will be controlling the steering) to point in the direction of the HC-SR04 that give the highest value (which will hopefully be the direction that is most 'obstruction free' :o :D ).

I have other stuff planed for the program so that it can actually move and know when to take measurements again but I will look into that once I have got this section completed.

The bit that I am needing to know is how to get the program to set the servo to the position that is associated with the HC-SRO4 that gave the highest distance value (either the left, middle or right)

I will have a go at the bits you guys have suggested and post a revised copy of my code in a little bit, Arrays seem like a very useful tool so I will have a go with them @PaulS. I don't particularly understand the code that @Robin2 posted.

iMakeFings: I don't particularly understand the code that @Robin2 posted.

All it does is make a note of the number of the sensor that has the highest value.

...R

You need to change your order of operations, and do each individual ‘ping’ and ‘pulseIn()’ call in turn.
ping…pulseIn()
ping…pulseIn()
ping…pulseIn()

I do a similar thing with my robot car, but I mounted a single ‘ping’ sensor on a servo, and swing it back and forth while taking distance readings, then the car turns in the direction with the longest clear path. (I take 5 readings - left, mid-left, centre, mid-right and right.)

Edit: I should have added, have you looked at the “NewPing” library? It makes things easier, and can return more consistent results, as well as automatically converting the time to a distance in inches or cm.