# IF Sensor Greater Than Multiple Other Sensors by X

4 distance sensors resulting in cm

Not sure how to write an if statement for the following result

IF Sensor1 is greater than Sensor2 and than Sensor3 and than Sensor4 each by more than 2cm then…do stuff
IF Sensor2 is greater than Sensor1 and than Sensor3 and than Sensor4 each by more than 2cm then…do stuff
IF Sensor3 is greater than Sensor1 and than Sensor2 and than Sensor4 each by more than 2cm then…do stuff
IF Sensor4 is greater than Sensor1 and than Sensor2 and than Sensor3 each by more than 2cm then…do stuff

These would be separate IF statements (they are each on their own) in a sequence so they don’t need compare to each other if/else.

Assume generous mounds of thanks for any help

basic if argument works

``````int Sensor1 = 34;
int Sensor2 = 38;
int Sensor3 = 14 ;
int Sensor4 = 31;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
// IF Sensor1 is greater than Sensor2 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor2 is greater than Sensor1 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor3 is greater than Sensor1 and than Sensor2 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor4 is greater than Sensor1 and than Sensor2 and than Sensor3 each by more than 2cm then...do stuff

if (Sensor1 > Sensor2 + 2 && Sensor1 > Sensor3 + 2 && Sensor1 > Sensor4 + 2) {
Serial.println("sensor1");
}
if (Sensor2 > Sensor1 + 2 && Sensor2 > Sensor3 + 2 && Sensor2 > Sensor4 + 2) {
Serial.println("sensor2");
}
if (Sensor3 > Sensor1 + 2 && Sensor3 > Sensor2 + 2 && Sensor3 > Sensor4 + 2) {
Serial.println("sensor3");
}
if (Sensor4 > Sensor1 + 2 && Sensor4 > Sensor2 + 2 && Sensor4 > Sensor3 + 2) {
Serial.println("sensor4");
}
//the +2 will have to be adjusted based on what scale 2cm is
//kinda new at this so i think on floats you have to + 2.00
//maybe wrong

}
``````

gpop1: basic if argument works

``````int Sensor1 = 34;
int Sensor2 = 38;
int Sensor3 = 14 ;
int Sensor4 = 31;

``````

void setup() {   // put your setup code here, to run once:   Serial.begin(9600); }

void loop() {   // put your main code here, to run repeatedly:   // IF Sensor1 is greater than Sensor2 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff   //IF Sensor2 is greater than Sensor1 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff   //IF Sensor3 is greater than Sensor1 and than Sensor2 and than Sensor4 each by more than 2cm then...do stuff   //IF Sensor4 is greater than Sensor1 and than Sensor2 and than Sensor3 each by more than 2cm then...do stuff

/[b]/ and to keep track just add BEFORE you do all of this

Serial.println("sensor1"); Serial.println(sensor1); Serial.println("sensor2"); Serial.println(sensor2); Serial.println("sensor3"); Serial.println(sensor3); Serial.println("sensor4"); Serial.println(sensor4); [/b]

if (Sensor1 > Sensor2 + 2 && Sensor1 > Sensor3 + 2 && Sensor1 > Sensor4 + 2) {

// actually I would put it here also to check if it actually work under what conditions Serial.println("sensor1"); Serial.println(sensor1); Serial.println("sensor2"); Serial.println(sensor2); Serial.println("sensor3"); Serial.println(sensor3); Serial.println("sensor4"); Serial.println(sensor4);

Serial.println("sensor1");   }   if (Sensor2 > Sensor1 + 2 && Sensor2 > Sensor3 + 2 && Sensor2 > Sensor4 + 2) {     Serial.println("sensor2");   }Serial.println("sensor1");   if (Sensor3 > Sensor1 + 2 && Sensor3 > Sensor2 + 2 && Sensor3 > Sensor4 + 2) {     Serial.println("sensor3");   }   if (Sensor4 > Sensor1 + 2 && Sensor4 > Sensor2 + 2 && Sensor4 > Sensor3 + 2) {     Serial.println("sensor4");   }   //the +2 will have to be adjusted based on what scale 2cm is   //kinda new at this so i think on floats you have to + 2.00   //maybe wrong else { // or even here to see the "default" Serial.println("None of the above "); Serial.println("sensor1"); Serial.println(sensor1); Serial.println("sensor2"); Serial.println(sensor2); Serial.println("sensor3"); Serial.println(sensor3); Serial.println("sensor4"); Serial.println(sensor4); }

}

well pointed out Vaclav. That idea sucked. So whats a better way?

As simple as +2 My mind wouldn't think that would work for some reason. Thus why we collaborate here. Thank you!

seems a peach of a problem. I can make it work but 70 lines of code seems to much for a simple problem.

still works by adding 2 just made sure I didn't corrupt the sensor data doing it.

``````int Sensor1 = 34;
int Sensor2 = 38;
int Sensor3 = 36 ;
int Sensor4 = 43;
int scale=2;
int test1;
int test2;
int test3;
int test4;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
// IF Sensor1 is greater than Sensor2 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor2 is greater than Sensor1 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor3 is greater than Sensor1 and than Sensor2 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor4 is greater than Sensor1 and than Sensor2 and than Sensor3 each by more than 2cm then...do stuff

Serial.print("sensor 1 ");
Serial.println(Sensor1);
Serial.print("sensor 2 ");
Serial.println(Sensor2);
Serial.print("sensor 3 ");
Serial.println(Sensor3);
Serial.print("sensor 4 ");
Serial.println(Sensor4);

test1=Sensor1;
test2=Sensor2;
test3=Sensor3;
test4=Sensor4;
test1=test1-scale;
test2=test2-scale;
test3=test3-scale;
test4=test4-scale;

if (test1 > Sensor2  && test1 > Sensor3  && test1 > Sensor4) {
Serial.println("sensor1  run program");
}
if (test2 > Sensor1 && test2 > Sensor3  && test2 > Sensor4 ) {
Serial.println("sensor2  run program");
}
if (test3 > Sensor1  && test3 > Sensor2  && test3 > Sensor4) {
Serial.println("sensor3  run program");
}
if (test4 > Sensor1  && test4 > Sensor2  && test4 > Sensor3 ) {
Serial.println("sensor4  run program");
}

Serial.print("sensor 1 ");
Serial.println(Sensor1);
Serial.print("sensor 2 ");
Serial.println(Sensor2);
Serial.print("sensor 3 ");
Serial.println(Sensor3);
Serial.print("sensor 4 ");
Serial.println(Sensor4);

}
``````

gpop1: seems a peach of a problem. I can make it work but 70 lines of code seems to much for a simple problem.

..and I can name that tune in two notes... I don't think this is a problem , I am just a stickler for making sure the code does what it suppose to do and I WANNA SEE it!

As far as 70 lines of code goes I use preprocessor during debugging and really don't care how many lines of code as long as I am bellow 90 % usage of memory. Knowing that the code does what I want it to do is more important and avoids "It does not work, help" dilemma.

Just for illustration - just finished a pseudo random generator of US amateur radio callsign - about 700 lines of debugging code. The actual code is probably less than 50 lines. Just bunch of random stuff and string concatenations. But it looks cool on LCD!

Back to OP - he did an excellent job describing the requirements and you just codded it "as is". Is there a better way - undoubtedly - there is probably some common denominator there. If you can put it in Karnaugh (sic?) map it will probably show better solution. But than you will have a convoluted code nobody can read - KISS! And speed is irrelevant !

ok im a little confussed as I can not make the original code I posted fail.

It works and doesn't affect the sensor data......what did you see that Im missing

``````int Sensor1 = 41;
int Sensor2 = 38;
int Sensor3 = 44;
int Sensor4 = 31;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
// IF Sensor1 is greater than Sensor2 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor2 is greater than Sensor1 and than Sensor3 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor3 is greater than Sensor1 and than Sensor2 and than Sensor4 each by more than 2cm then...do stuff
//IF Sensor4 is greater than Sensor1 and than Sensor2 and than Sensor3 each by more than 2cm then...do stuff

Serial.println("Sensor1");
Serial.println(Sensor1);
Serial.println("Sensor2");
Serial.println(Sensor2);
Serial.println("Sensor3");
Serial.println(Sensor3);
Serial.println("Sensor4");
Serial.println(Sensor4);

if (Sensor1 > Sensor2 + 2 && Sensor1 > Sensor3 + 2 && Sensor1 > Sensor4 + 2) {

// actually I would put it here also to check if it actually work under what conditions
Serial.println("Sensor1");
Serial.println(Sensor1);
Serial.println("Sensor2");
Serial.println(Sensor2);
Serial.println("Sensor3");
Serial.println(Sensor3);
Serial.println("Sensor4");
Serial.println(Sensor4);

Serial.println("Sensor1 is highest ");
}
else if (Sensor2 > Sensor1 + 2 && Sensor2 > Sensor3 + 2 && Sensor2 > Sensor4 + 2) {
Serial.println("Sensor2 is highest");
}
else if (Sensor3 > Sensor1 + 2 && Sensor3 > Sensor2 + 2 && Sensor3 > Sensor4 + 2) {
Serial.println("Sensor3 is highest");
}
else if (Sensor4 > Sensor1 + 2 && Sensor4 > Sensor2 + 2 && Sensor4 > Sensor3 + 2) {
Serial.println("Sensor4 is highest");
}
//the +2 will have to be adjusted based on what scale 2cm is
//kinda new at this so i think on floats you have to + 2.00
//maybe wrong
else
{
// or even here to see the "default"
Serial.println("None of the above ");
Serial.println("Sensor1");
Serial.println(Sensor1);
Serial.println("Sensor2");
Serial.println(Sensor2);
Serial.println("Sensor3");
Serial.println(Sensor3);
Serial.println("Sensor4");
Serial.println(Sensor4);
}

}
``````

I don’t see a problem even in floats. so I give up.

``````float Sensor1 = 50.01;
float Sensor2 = 38.25;
float Sensor3 = 44.45;
float Sensor4 = 48.01;
float cm2=2.00;//scale to what ever is 2 cm
void setup() {
Serial.begin(9600);
}

void loop() {

if (Sensor1 >= Sensor2 + cm2 && Sensor1 >= Sensor3 + cm2 && Sensor1 >= Sensor4 + cm2) {
Serial.println("Sensor1 is higher by 2cm ");
}
else if (Sensor2 >= Sensor1 + cm2 && Sensor2 >= Sensor3 + cm2 && Sensor2 >= Sensor4 + cm2) {
Serial.println("Sensor2 is higher by 2cm ");
}
else if (Sensor3 >= Sensor1 + cm2 && Sensor3 >= Sensor2 + cm2 && Sensor3 >= Sensor4 + cm2) {
Serial.println("Sensor3 is higher by 2cm ");
}
else if (Sensor4 >= Sensor1 + cm2 && Sensor4 >= Sensor2 + cm2 && Sensor4 >= Sensor3 + cm2) {
Serial.println("Sensor4 is higher by 2cm ");
}
else
{
Serial.println("No sensor is cm2 larger");
}
}
``````