compare statement

Hello everyone,

I want to check if two variables are within certain ranges and if they both are, then do something.
the limits of the ranges are stored as array elements.
I wrote the following statment but it’s not working, is there anything wrong with the if statement? or with the whole code.

for(byte v=0;v<num;v++)
    {
      if((templat<=A[v] && templat>=LatB[v])&&(templon<=LonA[v] && templon>=LonB[v]))
      {
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("case 1");
        dataFile.close();
        break;
      }
    }

or with the whole code.

You didn't post the whole code, or even enough for us to say what the problem is.

I would be adding Serial.print() statements to print out the value in templat before the for loop, and of A[v], latA[v], and lonB[v] in the loop.

I always test that the value is greater than the lower end and less that the upper end, because that feels more logical to me. Testing that the value is less than the upper end and greater than the lower end works, but requires that I do more thinking.

it’s not working

That is a very lame thing to say. What should it do and what does it do ?
Have you tried printing the variables that are being tested to see wheyher the values are what you think ?

Adding some white space in the expressions would make it easier to read too

for (byte v = 0; v < num; v++)
{
  if ((templat <= A[v] && templat >= LatB[v]) && (templon <= LonA[v] && templon >= LonB[v]))
  {
    dataFile = SD.open("pvalues.txt", FILE_WRITE);
    dataFile.println("case 1");
    dataFile.close();
    break;
  }
}

so first I'm importing the limits and then I print them on a txt file, and they are identical. also I'm printing the variables that are being checked and they are within the ranges, still the printing of "case 1" isn't executed.

I also tried to change the order of the limits like PaulS suggested but it didn't work niether

I also tried to change the order of the limits like PaulS suggested but it didn't work niether

We STILL have no data to understand the problem. Bring us some. Quit whining.

Why is this line in the "if" statement body? What do you think it does?

break;

These are the importing and the comparing parts. Both latAbiggerlatB and lonAbiggerlonB are defined as global bool variables

void importLimits()
{
  File dataFile;
  dataFile = SD.open("Limits.txt", FILE_READ);
  if(dataFile)
  {
    for(byte g=0;g<num;g++)
    {
      LatA[g] = dataFile.parseInt();
      LonA[g] = dataFile.parseInt();
      LatB[g] = dataFile.parseInt();
      LonB[g] = dataFile.parseInt();
    }
    dataFile.close();
  }
  if(LatA[0] > LatB[0])
  {
    bool latAbiggerlatB = true;
    dataFile = SD.open("pvalues.txt", FILE_WRITE);
    dataFile.println("LatA > LatB");
    dataFile.close();
    }
    else
    {
      bool latAbiggerB = false;
      dataFile = SD.open("pvalues.txt", FILE_WRITE);
      dataFile.println("LatA < LatB");
      dataFile.close();
      }
      if(LonA[0] > LonB[0])
      {
        bool lonAbiggerlonB = true;
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("LonA > LonB");
        dataFile.close();
        }
        else
        {
          bool lonAbiggerlonB = false;
          dataFile = SD.open("pvalues.txt", FILE_WRITE);
          dataFile.println("LonA < LonB");
          dataFile.close();
        }
}
void Compare()
{
  if(latAbiggerlatB == true && lonAbiggerlonB == true)
  {
    for(byte v=0;v<num;v++)
    {
      if((templat >= LatB[v] && templat <= LatA[v]) && (templon >= LonB[v] && templon <= LonA[v]))
      {
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("case1");
        dataFile.close();
        break;
      }
    }
  }
  if(latAbiggerlatB == true && lonAbiggerlonB == false)
  {
    for(byte b=0;b<num;b++)
    {
      if((templat >= LatB[b] && templat <= LatA[b]) && (templon >= LonA[b] && templon <= LonB[b]))
      {
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("case2");
        dataFile.close();
        break;
      }
    }
  }
  if(latAbiggerlatB == false && lonAbiggerlonB == true)
  {
    for(byte n=0;n<num;n++)
    {
      if((templat >= LatA[n] && templat <= LatB[n]) && (templon >= LonB[n] && templon <= LonA[n]))
      {
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("case3");
        dataFile.close();
        break;
      }
    }
  }
  if(latAbiggerlatB == false && lonAbiggerlonB == false)
  {
    for(byte m=0;m<num;m++)
    {
      if((templat >= LatA[m] && templat <= LatB[m]) && (templon >= LonA[m] && templon <= LonB[m]))
      {
        dataFile = SD.open("pvalues.txt", FILE_WRITE);
        dataFile.println("case4");
        dataFile.close();
        break;
      }
    }
  }
}

We STILL have no data to understand the problem. Bring us some. Quit whining.

Both latAbiggerlatB and lonAbiggerlonB are defined as global bool variables

But

    bool latAbiggerlatB = true;
      bool latAbiggerB = false;
        bool lonAbiggerlonB = true;
          bool lonAbiggerlonB = false;

are local variables that mask them, and go out of scope.

  if(latAbiggerlatB == true && lonAbiggerlonB == true)

Now, you are using the global variables that you have not assigned meaningful values to.