figure the difference between 2 DHT11 sensors in %

So i want to build some failsafe in to my sketch

if the values from the 2 sensors differs more than a given percentage i want to sound an alarm

my sketch so fair.. main loop only

void loop()
{
 float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();


// check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  } else {
    Serial.print("Humidity 1: "); 
    Serial.print(h1);
    Serial.print(" %\t");
    Serial.print("Temperature 1: "); 
    Serial.print(t1);
    Serial.println(" *C");
  }
  if (isnan(t2) || isnan(h2)) {
    Serial.println("Failed to read from DHT #2");
  } else {
    Serial.print("Humidity 2: "); 
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperature 2: "); 
    Serial.print(t2);
    Serial.println(" *C");
  }
  Serial.println();

i know how to calculate % difference, but in this case i'm stuck in my head as i would never know which of the 2 that has the highest temps

also i ask to get the most optimal way to get what i want

boelle:
i would never know which of the 2 that has the highest temps

if(t1 > t2){
    // t1 is higher
} 
else {
   // t2 is higher
}

Doesn't get any simpler than that.

oki, you beat me to it

i found another way

//failsafe

float maxtemp = max(t1, t2); // the higher of the 2 temps

float difftemp = ((t1-t2)/maxtemp )*100 //the difference in percentage

boelle:
float difftemp = ((t1-t2)/maxtemp )*100 //the difference in percentage

If you're ok with potentially getting a negative answer.

oh i see… facepalm

maybe a little "overkill"

float maxtemp = max(t1, t2); // highest temp from the 2 sensors
float mintemp = min(t1, t2; // lowest temp from the 2 sensors

float difftemp = ((maxtemp-mintemp)/maxtemp)*100; //the difference in percentage

float difftemp = (abs(t1 - t2)/max(t1, t2))*100;

i feel "retarded" :confused:

but thanks

q.. would my code have worked?

boelle:
q.. would my code have worked?

Yes, I just condensed it.

thanks for that.. at least i learned something new

(deleted)