IF ELSE statement with Arrrays

i need help with my programming. i'm having problem with the if-else statement to be fitted into my program.

int sensorValue = 0;
float voltage = 0;
int count = 0;
int Rdiff = 0;
int DeltaT = 0;
int T = 0;
int myArray[16]={42506,33892,27219,22021,17926,14674,12081,10000,8315,6948,5834,4917,4161,3535,3014,1};

unsigned long T1, T2, Thermistor_R;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  sensorValue = analogRead(A0);
  
  Serial.println("The digital value at A0 is");
  Serial.println(sensorValue);
  
  voltage = sensorValue*(5.0/1023);
  
  Serial.println("The analog voltage at A0 is ");
  Serial.println(voltage);
  
  T1 = (5.0*1000)/voltage;
  T2 = T1 - 1000;
  Thermistor_R = 10000000/T2;
  
  Serial.println("The resistance value of the thermistor is");
  Serial.println(Thermistor_R);
  
  delay(3000);   
 
  if(Thermistor_R < count = 0 && Thermistor_R > count+1)
    {
      Rdiff = R(count)-R(count+1);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = -10 + DeltaT;
    }  
      else(Thermistor_R < R(count+1) && Thermistor_r > R(count+2))
    {  
      Rdiff = R(count+1)-R(count+2);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = -5 + DeltaT;
    }  
      else(Thermistor_R < R(count+2) && Thermistor_r > R(count+3))
    { 
      Rdiff = R(count+2)-R(count+3);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 0 + DeltaT;
    }  
      else(Thermistor_R < R(count+3) && Thermistor_r > R(count+4))
    {
      Rdiff = R(count+3)-R(count+4);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 5 + DeltaT;
    }  
      else(Thermistor_R < R(count+4) && Thermistor_r > R(count+5))
    { 
      Rdiff = R(count+4)-R(count+5);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 10 + DeltaT;
    }  
      else(Thermistor_R < R(count+5) && Thermistor_r > R(count+6))
    { 
      Rdiff = R(count+5)-R(count+6);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 15 + DeltaT;
    }  
      else(Thermistor_R < R(count+6) && Thermistor_r > R(count+7))
    { 
      Rdiff = R(count+6)-R(count+7);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 20 + DeltaT;
    }  
      else(Thermistor_R < R(count+7) && Thermistor_r > R(count+8))
    { 
      Rdiff = R(count+7)-R(count+8);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 25 + DeltaT;
    }  
      else(Thermistor_R < R(count+8) && Thermistor_r > R(count+9))
    { 
      Rdiff = R(count+8)-R(count+9);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 30 + DeltaT;
    }  
      else(Thermistor_R < R(count+9) && Thermistor_r > R(count+10))
    { 
      Rdiff = R(count+9)-R(count+10);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 35 + DeltaT;
    }  
      else(Thermistor_R < R(count+10) && Thermistor_r > R(count+11))
    { 
      Rdiff = R(count+10)-R(count+11);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 40 + DeltaT;
    }  
      else(Thermistor_R < R(count+11) && Thermistor_r > R(count+12))
    { 
      Rdiff = R(count+11)-R(count+12);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 45 + DeltaT;
    }  
      else(Thermistor_R < R(count+12) && Thermistor_r > R(count+13))
    { 
      Rdiff = R(count+12)-R(count+13);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 50 + DeltaT;
    }  
      else(Thermistor_R < R(count+13) && Thermistor_r > R(count+14))
    { 
      Rdiff = R(count+13)-R(count+14);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 55 + DeltaT;
    }  
      else(Thermistor_R < R(count+14) && Thermistor_r > R(count+15))
    { 
      Rdiff = R(count+14)-R(count+15);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 60 + DeltaT;
    }  
      
      Thermistor_Temp(0) = -10;
      Thermistor_R(0) = 42506;   
     
      Thermistor_Temp(1) = -5;
      Thermistor_R(1) = 33892;
     
      Thermistor_Temp(2) = 0;
      Thermistor_R(2) = 27219;
     
      Thermistor_Temp(3) = 5;
      Thermistor_R(3) = 22021;
     
      Thermistor_Temp(4) = 10;
      Thermistor_R(4) = 17926;
     
      Thermistor_Temp(5) = 15;
      Thermistor_R(5) = 14674;
     
      Thermistor_Temp(6) = 20;
      Thermistor_R(6) = 12081;
     
      Thermistor_Temp(7) = 25;
      Thermistor_R(7) = 10000;
     
      Thermistor_Temp(8) = 30;
      Thermistor_R(8) = 8315;
     
      Thermistor_Temp(9) = 35;
      Thermistor_R(9) = 6948;
     
      Thermistor_Temp(10) = 40;
      Thermistor_R(10) = 5834;
     
      Thermistor_Temp(11) = 45;
      Thermistor_R(11) = 4917;
     
      Thermistor_Temp(12) = 50;
      Thermistor_R(12) = 4161;
     
      Thermistor_Temp(13) = 55;
      Thermistor_R(13) = 3535;
     
      Thermistor_Temp(14) = 60;
      Thermistor_R(14) = 3014;
     
      Thermistor_Temp(15) = 1000;
      Thermistor_R(15) = 1;     
      
}

anybody would be able to help? =)

if(Thermistor_R < count = 0 && Thermistor_R > count+1)
What are you trying to do here?

Rdiff = R(count)-R(count+1);
And here too. What is R?

:sleeping: Time

.

First of all those numbers are too big to fit into an int type array.

Next every else must have a matching if, your code does not.

Maybe you need to use else if

hi guys. im actually trying to read the thermistor's resistance, therefore Thermistor_r is the resistance of the thermistor. so i have this array's details.

      Thermistor_Temp(0) = -10;
      Thermistor_R(0) = 42506;   
     
      Thermistor_Temp(1) = -5;
      Thermistor_R(1) = 33892;
     
      Thermistor_Temp(2) = 0;
      Thermistor_R(2) = 27219;
     
      Thermistor_Temp(3) = 5;
      Thermistor_R(3) = 22021;
     
      Thermistor_Temp(4) = 10;
      Thermistor_R(4) = 17926;
     
      Thermistor_Temp(5) = 15;
      Thermistor_R(5) = 14674;
     
      Thermistor_Temp(6) = 20;
      Thermistor_R(6) = 12081;
     
      Thermistor_Temp(7) = 25;
      Thermistor_R(7) = 10000;
     
      Thermistor_Temp(8) = 30;
      Thermistor_R(8) = 8315;
     
      Thermistor_Temp(9) = 35;
      Thermistor_R(9) = 6948;
     
      Thermistor_Temp(10) = 40;
      Thermistor_R(10) = 5834;
     
      Thermistor_Temp(11) = 45;
      Thermistor_R(11) = 4917;
     
      Thermistor_Temp(12) = 50;
      Thermistor_R(12) = 4161;
     
      Thermistor_Temp(13) = 55;
      Thermistor_R(13) = 3535;
     
      Thermistor_Temp(14) = 60;
      Thermistor_R(14) = 3014;
     
      Thermistor_Temp(15) = 1000;
      Thermistor_R(15) = 1;

if my formula for this program.

R(difference) = R(0) - R(1) //42506 - 33892

DeltaT = R(difference)
------------------ X5 degrees
Thermistor_R

Temperature read = -10 + DeltaT

Temperature read is what i have to find out.

I agree with Mike here.

  if(Thermistor_R < count = 0 && Thermistor_R > count+1)

OK.

If A < B and also A > B ...

When would that ever happen?

count = 0

count is now zero.

So your "if" reads:

 if(Thermistor_R < 0 && Thermistor_R > 1)

Or to make it simpler:

If X < 0 and also X > 1 then do something ...

Couple of probems.

First:

      else(Thermistor_R < R(count+12) && Thermistor_r > R(count+13))
    {
      Rdiff = R(count+12)-R(count+13);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 50 + DeltaT;
    } 
      else(Thermistor_R < R(count+13) && Thermistor_r > R(count+14))

What happens when Thermistor_r is exactly R(cont+13)?

Next, syntax for if/else is

  if(condition)
    statement
  else
    statement

That statement can be:

  • an expression terminated with a semicolon
  • a block od statements surrounded with braces
  • another if statement

And spacing and indenting doesn't matter - we only space things and indent blocxks so that humans can read it.

so to get this to compile, you want:

    if(Thermistor_R < count = 0 && Thermistor_R > count+1) {
      Rdiff = R(count)-R(count+1);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = -10 + DeltaT;
    } 
    else 
    if(Thermistor_R < R(count+1) && Thermistor_r > R(count+2)) {
    }
    ... etc
    else if(Thermistor_R < R(count+12) && Thermistor_r > R(count+13))
    {
      Rdiff = R(count+12)-R(count+13);
      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 50 + DeltaT;
    } 
    ... etc

If the values returrned by R are descending (which they would have to be for your code to work at all), then you only need to check one condition.

    ... etc
    if(Thermistor_R > R(count+1)) {
    }
    else if(Thermistor_R > R(count+2)) {
    }
    else if(Thermistor_R > R(count+3)) {
    }
    else if(Thermistor_R > R(count+4)) {
    }

Think about it.

These lines are repeated

      DeltaT = (Rdiff/Thermistor_R)*5;
      T = 35 + DeltaT;

And you couls much better pull them out to the end of the chain of if-elses.

Your chain of if-elses is much better off done with a loop:

for(int i = 0; i<14;i++) {
  if(Thermistor_R > R(count+i)) {
    Rdiff = R(count+i)-R(count+i+1);
    DeltaT = (Rdiff/Thermistor_R)*5;
    T = -10 + (i*5) + DeltaT;
    break;
  }
}

If R() is a function, maybe R(n)-R(n+1) can be computed more directly. (If R is a spline, then fair enough).

You are initializing Thermistor_Temp and Thermistor_R each pass through the loop.

And now I see that these are supposed to be arrays, not functions, so the syntax is square brackets not parenthesis.

Dude, try this. This is an example of using a binary chop search. Make an effort to understand what it is doing - it will be worth your while.

const int N_SAMPLES = 16;

const float tempSample[N_SAMPLES] = {
  -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 1000
};

const float resistanceSample[N_SAMPLES] = {
  42506.0, 33892.0, 27219.0, 22021.0, 17926.0, 14674.0, 12081.0,
  10000.0, 8315.0, 6948.0, 5834.0, 4917.0, 4161.0, 3535.0, 3014.0, 1.0
};

float calculatetemp(float resistance) {
  if (resistance >= resistanceSample[0]) {
    return tempSample[0];
  }

  if (resistance < resistanceSample[N_SAMPLES - 1]) {
    return tempSample[N_SAMPLES - 1];
  }

  int lowerIndex = 0;
  int upperIndex = N_SAMPLES - 1;

  while (upperIndex - lowerIndex > 1) {
    
    int testIndex = (upperIndex + lowerIndex) / 2;
    if (resistance > resistanceSample[testIndex]) {
      upperIndex = testIndex;
    }
    else {
      lowerIndex = testIndex;
    }
  }

  return
    tempSample[lowerIndex] +
    (tempSample[upperIndex] - tempSample[lowerIndex]) *
    (resistance - resistanceSample[lowerIndex]) /
    (resistanceSample[upperIndex] - resistanceSample[lowerIndex]);

}


void setup()
{
  Serial.begin(9600);
  
  // give me 5 sec to get the serial monitor up :)
  for(int i = 5; i>=0;i--) {
    delay(1000);
    Serial.print(' ');
    Serial.print(i);
  }  
  Serial.println();

  for(float r = resistanceSample[0]*5; r > resistanceSample[N_SAMPLES-1]/5; r /= 1.5) {
    Serial.print("resistance ");
    Serial.print(r);
    Serial.print(" temp ");
    Serial.print(calculatetemp(r));
    Serial.println();
  } 
  
}

void loop()
{
}