# Filtering highest value out of 5 analog pins

Hello,

Is there anybody who know how I can filter the highest value out of 5 analog pins. This value is between +/-0 and +/-600. I need this for my robot. It has 5 distence sensors to messure the distance and it will write this to an analog read value in the Arduino. The Arduino uno had to filter the highest value out of the 5 to stop, always the shortest one (highest value) should get priority.

Kind regards, The Arduino Buggy team.

Use if statements, Or the method used here http://stackoverflow.com/questions/2053843/min-and-max-value-of-data-type-in-c

If I take if states, I guess it will become very long and slow.

No.
You could also put the readings into an array and simply search through the array looking for the biggest one.

``````for(int i =0; i<5; i++){
if( val[i] > biggest) biggest = val[i];
}
``````

Set biggest to your lowest value first and put all your readings in an array called val

Hello,

This is what I have at the moment,

``````const int pot1 = A0;
const int pot2 = A1;
const int pot3 = A2;
const int pot4 = A3;
const int pot5 = A4;

int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
int value5 = 0;

int valueHighest = 0;
byte highest = 0;

int count = 0;

void setup(){
pinMode(pot1, INPUT);
pinMode(pot2, INPUT);
pinMode(pot3, INPUT);
pinMode(pot4, INPUT);
pinMode(pot5, INPUT);

Serial.begin(9600);
}

void loop(){
for(count = 0; count < 1023; count++){

if((value1 == count) && (value1 > valueHighest)){
valueHighest = value1;
highest = 1;
}
if((value2 == count) && (value2 > valueHighest)){
valueHighest = value2;
highest = 2;
}
if((value3 == count) && (value3 > valueHighest)){
valueHighest = value3;
highest = 3;
}
if((value4 == count) && (value4 > valueHighest)){
valueHighest = value4;
highest = 4;
}
if((value5 == count) && (value5 > valueHighest)){
valueHighest = value5;
highest = 5;
}

if(count == 1023){
count = 0;
highest = 0;
}
Serial.println(highest);
Serial.print("Input 1");
Serial.println(value1);
Serial.print("Input 2");
Serial.println(value2);
Serial.print("Input 3");
Serial.println(value3);
delay(1000);
}
}
``````

This is what I have at the moment,

Scrap that and do what Mike showed, except don't mix 'i' and 'I' :)

Rob

except don't mix 'i' and 'I'

dammed iPad fonts, they both look the same it in. :0

Now fixed. :)

I am now trying with that but still having problems. It won’t save the data into the array.

``````/*

----------------------------------------------------------------------
Sensor schakeling voor ATmega328
----------------------------------------------------------------------
Titel:               Sensor_VXX.ino
Auteur:              Kenny.quintens
Datum Gemaakt:       08/05/12
Laatste Aanpassing:  08/05/12
Doel:                IR sensoren aansturen die in de buggy zitten.

Compiled met Arduino 1.0

Herziening lijst:

*/

/*----------------------------------------------------------------------
Libraries
--------------------------------------------------------------------*/

/*----------------------------------------------------------------------
Ingang
----------------------------------------------------------------------
Type               Naam                Pin
--------------------------------------------------------------------*/
int sensorIR[] = {
A0, A1, A2, A3, A4};  // The input sensor signals

/*--------------------------------------------------------------------
Uitgang
----------------------------------------------------------------------
Type               Naam                 Pin
--------------------------------------------------------------------*/
const int luidspreker = 12;
const int LED         = 13;

/*--------------------------------------------------------------------
Nota's
----------------------------------------------------------------------
- Deze code mag vrijblijvend aangepast worden, maar vermeld me steeds tussen de auteur lijst.
*/

/*--------------------------------------------------------------------
Globale Variabele
--------------------------------------------------------------------*/
int sensorValue[] = {
0, 0, 0, 0, 0};    // The values to store the input signals
int valueIR  =  0;
byte highestSensor = 0;
int threshold = 100;
int sampleTime = 50;
int fase = 0;
int nextSensor = 0;

/*--------------------------------------------------------------------
Initialiseren
--------------------------------------------------------------------*/
void setup(){
Serial.begin(9600);

for( int i = 0; i <5; i++){
pinMode(sensorIR[i], INPUT);
}

pinMode(luidspreker, OUTPUT);
pinMode(LED, OUTPUT);

buggyStart();
Serial.print("Buggy klaar");
}

void loop(){
//warningSignal();

if(sensorValue[nextSensor] > valueIR){
sensorValue[nextSensor] = valueIR;
highestSensor = nextSensor;
}
/*
if(threshold > valueIR){
highestSensor = 0;
valueIR = 0;
}*/

delay(sampleTime);

if(nextSensor > 4){
nextSensor = 0;
}

/*
Serial.print("valueIR: ");
Serial.print(valueIR);
Serial.println("");

Serial.print("fase: ");
Serial.print(fase);
Serial.println("");
*/
Serial.println("sensorValue[0]");
Serial.println(sensorValue[0]);
Serial.println("sensorValue[1]");
Serial.println(sensorValue[1]);
Serial.println("sensorValue[2]");
Serial.println(sensorValue[2]);
Serial.println("sensorValue[3]");
Serial.println(sensorValue[3]);
Serial.println("sensorValue[4]");
Serial.println(sensorValue[4]);
}

void buggyStart(){
//digitalWrite(luidspreker, HIGH);
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(luidspreker, LOW);
digitalWrite(LED, LOW);
delay(1000);
}

if(sensorValue[nextSensor] > valueIR){
sensorValue[nextSensor] = valueIR;
highestSensor = nextSensor;
}
/*
if(threshold > valueIR){
highestSensor = 0;
valueIR = 0;
}*/

delay(sampleTime);

if(nextSensor > 4){
nextSensor = 0;
}

}

void warningSignal(){
if(valueIR <= 100){
digitalWrite(luidspreker, LOW);
digitalWrite(LED, LOW);

fase = 1;
}

if((valueIR > 100) && (valueIR <= 150)){
digitalWrite(luidspreker, LOW);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);

fase = 2;
}

if((valueIR >= 150) && (valueIR < 500)){
digitalWrite(luidspreker, HIGH);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);

fase = 3;
}

if(valueIR >= 500){
digitalWrite(luidspreker, LOW);
digitalWrite(LED, LOW);
fase = 4;
}
}
``````

sorry for the dutch in it that’s my original language

Where does nextSensor get incremented?

Learn to use arrays, keeps code compact

give it a try

``````int pot[5] = { A0, A1, A2, A3, A4 };
int value[5];
int valueHighest = 0;
byte highest = 0;

int count = 0;

void setup()
{
for (int i=0; i<5; i++)
{
pinMode(pot[i], INPUT);
}
Serial.begin(9600);
}

void loop()
{
valueHighest = 0;
highest = 0;
for (int i=0; i<5; i++)
{
if (value[i] > valueHighest)
{
valueHighest = value[i];
highest = i;
}
}

Serial.println(highest);
for (int i=0; i<5; i++)
{
Serial.print("Input ");
Serial.print(i);
Serial.print(" : ");
Serial.println(value[i]);
}
delay(1000);
}
``````

But won't I get problems with that for loop, because it stay's in the loop till i is 5. So the rest of my program won't continue and it will cause that my robot will crash or wouldn't do what he has to do.

But won’t I get problems with that for loop, because it stay’s in the loop till i is 5.

It stays in that loop for about half a millisecond.
How fast is your robot moving?

1meter / second. But it has to follow a line with linesensors.

So we are talking about half a millimeter before it responds? It is not a CNC machine you know.