Having trouble on solving the following tasks

Here is my modified version of my code: -

const int ThermPin = A0;
const int rows = 10;
const int cols = 2;
int Table[rows][cols] ={   //col 0||col 1
                            { 25, 4470 }, //row 0
                            { 26, 4250 }, //row 1
                            { 27, 4030 }, //row 2
                            { 28, 3800 }, //row 3
                            { 29, 3530 }, //row 4
                            { 30, 3270 }, //row 5
                            { 31, 3200 }, //row 6
                            { 32, 3170 }, //row 7
                            { 33, 3100 }, //row 8
                            { 34, 3070 }  //row 9
                       };    
void setup()
{
  Serial.begin(9600);
}

void loop()
{

float Vin = 5.0;
float ThermResist = 0.0;
float R2 = 10000.0;
float SensorValue = 0.0;
float Vout = 0.0;
                         
  SensorValue = analogRead(ThermPin);
  Serial.print("Value = ");
  Serial.print(SensorValue);
  
  Vout = (((SensorValue+1)*Vin)/1024.0);
  Serial.print("\t Voltage = ");
  Serial.print(Vout);
  Serial.print(" V");
  
  ThermResist =((R2*Vin)/Vout)-R2;
  Serial.print("\t Resistance = ");
  Serial.print(ThermResist);
  Serial.println(" Ohm");
  
  for (int i = 0; i<rows; i++)
  {
   for(int j = 0;j<cols; j++)
   {
      if(ThermResist >= Table[2][1] && ThermResist <= Table[0][1] )
      {
        Serial.print(Table[0][0]);
        Serial.println(" C");     
      }
      if(ThermResist >= Table[4][1] && ThermResist <= Table[3][1] )
      {
        Serial.print(Table[3][0]); 
        Serial.println(" C");
      }
      if(ThermResist >= Table[6][1] && ThermResist <= Table[5][1] )
      {
        Serial.print(Table[5][0]);
        Serial.println(" C");
      }
      if(ThermResist >= Table[9][1] && ThermResist <= Table[7][1] )
      {
        Serial.print(Table[9][0]);
        Serial.println(" C");
      }
    i++;
   } 
  j++;
  }

}

And here is the output to that (Works like a charm :D!): -

Value = 685.00	 Voltage = 3.35 V	 Resistance = 4927.11 Ohm
Value = 688.00	 Voltage = 3.36 V	 Resistance = 4862.12 Ohm
Value = 693.00	 Voltage = 3.39 V	 Resistance = 4755.04 Ohm
Value = 695.00	 Voltage = 3.40 V	 Resistance = 4712.64 Ohm
Value = 698.00	 Voltage = 3.41 V	 Resistance = 4649.50 Ohm
Value = 701.00	 Voltage = 3.43 V	 Resistance = 4586.89 Ohm
Value = 704.00	 Voltage = 3.44 V	 Resistance = 4524.82 Ohm
Value = 707.00	 Voltage = 3.46 V	 Resistance = 4463.28 Ohm
25 C
25 C
25 C
25 C
25 C
25 C
25 C
Value = 729.00	 Voltage = 3.56 V	 Resistance = 4027.40 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 731.00	 Voltage = 3.57 V	 Resistance = 3989.07 Ohm
Value = 731.00	 Voltage = 3.57 V	 Resistance = 3989.07 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 739.00	 Voltage = 3.61 V	 Resistance = 3837.84 Ohm
Value = 737.00	 Voltage = 3.60 V	 Resistance = 3875.34 Ohm
Value = 739.00	 Voltage = 3.61 V	 Resistance = 3837.84 Ohm
Value = 740.00	 Voltage = 3.62 V	 Resistance = 3819.16 Ohm
Value = 740.00	 Voltage = 3.62 V	 Resistance = 3819.16 Ohm
Value = 742.00	 Voltage = 3.63 V	 Resistance = 3781.96 Ohm
28 C
28 C
28 C
28 C
28 C
28 C
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 768.00	 Voltage = 3.75 V	 Resistance = 3316.00 Ohm
Value = 768.00	 Voltage = 3.75 V	 Resistance = 3316.00 Ohm
Value = 769.00	 Voltage = 3.76 V	 Resistance = 3298.70 Ohm
Value = 771.00	 Voltage = 3.77 V	 Resistance = 3264.25 Ohm
30 C
30 C
30 C
30 C
30 C
30 C
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 777.00	 Voltage = 3.80 V	 Resistance = 3161.95 Ohm
34 C
34 C
34 C
34 C
34 C
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm

You're still missing the point of using the for loop to provide you with indices.

How am i suppose to announce the indices that i need to call out the printout that i am looking for (say i need to call out a printout of 25 C with a range between 4030 & 4470) How will i apply that in an index method?

I now cannot work out what you're trying to do.
If you're just filtering for specific values, then you don't need loops at all.

I thought you were using a for loop to give you lookup capabilities, but since you don't use the indices, you're just wasting processing cycles.
I also don't see why you need to iterate over the columns of the table at all.

I see your double increment has crept back in.

AWOL:
I now cannot work out what you're trying to do.
If you're just filtering for specific values, then you don't need loops at all.

I thought you were using a for loop to give you lookup capabilities, but since you don't use the indices, you're just wasting processing cyles.
I also don't see why you need to iterate over the columns of the table at all.

I see your double increment has crept back in.

That's the whole point of having the for loop...is to use it for lookup capabilities as you have mentioned.

i myself, don't like to use for loops as much to be honest with you.

Because, it appears, you do not understand them. They are really quite simple, and very, very useful. I think it would be worth the effort to understand them.

PaulS:

i myself, don't like to use for loops as much to be honest with you.

Because, it appears, you do not understand them. They are really quite simple, and very, very useful. I think it would be worth the effort to understand them.

Yes, i do know how to use them..but i don't know why do i need to use indeces for my if statements. This is the major problem that i am currently facing at this moment.

Yes, i do know how to use them

No you don't. If you knew, you'd scream seeing those i++ and j++ repeated at the end of the for code block.

but i don't know why do i need to use indeces for my if statements. This is the major problem that i am currently facing at this moment.

If you don't know why you should use them, then probably you don't need them. :wink:

tuxduino:

Yes, i do know how to use them

No you don't. If you knew, you'd scream seeing those i++ and j++ repeated at the end of the for code block.

but i don't know why do i need to use indeces for my if statements. This is the major problem that i am currently facing at this moment.

If you don't know why you should use them, then probably you don't need them. :wink:

What are you on about? i have removed those codes a while ago. and Still prints out the results based on the increments by 4-5 times based on the table.
Also, if i don't need the indeces, then what's the point of using the for loop then :S?

i have removed those codes a while ago.

They were still there in the last code you posted. You can hardly expect us to keep trying to help you if you are changing code without telling us.

What are you on about? i have removed those codes a while ago.

Please read again your reply #44.

Changing code that you posted a while ago is a dirty trick. Please do not do that again. Post the code in a new reply.

Here is my modified version of my code: -

const int ThermPin = A0;
const int rows = 10;
const int cols = 2;
int Table[rows][cols] ={   //col 0||col 1
                            { 25, 4470 }, //row 0
                            { 26, 4250 }, //row 1
                            { 27, 4030 }, //row 2
                            { 28, 3800 }, //row 3
                            { 29, 3530 }, //row 4
                            { 30, 3270 }, //row 5
                            { 31, 3200 }, //row 6
                            { 32, 3170 }, //row 7
                            { 33, 3100 }, //row 8
                            { 34, 3070 }  //row 9
                       };    
void setup()
{
  Serial.begin(9600);
}

void loop()
{

float Vin = 5.0;
float ThermResist = 0.0;
float R2 = 10000.0;
float SensorValue = 0.0;
float Vout = 0.0;
                         
  SensorValue = analogRead(ThermPin);
  Serial.print("Value = ");
  Serial.print(SensorValue);
  
  Vout = (((SensorValue+1)*Vin)/1024.0);
  Serial.print("\t Voltage = ");
  Serial.print(Vout);
  Serial.print(" V");
  
  ThermResist =((R2*Vin)/Vout)-R2;
  Serial.print("\t Resistance = ");
  Serial.print(ThermResist);
  Serial.println(" Ohm");
  
  for (int i = 0; i<rows; i++)
  {
   for(int j = 0;j<cols; j++)
   {
      if(ThermResist >= Table[2][1] && ThermResist <= Table[0][1] )
      {
        Serial.print(Table[0][0]);
        Serial.println(" C");     
      }
      if(ThermResist >= Table[4][1] && ThermResist <= Table[3][1] )
      {
        Serial.print(Table[3][0]); 
        Serial.println(" C");
      }
      if(ThermResist >= Table[6][1] && ThermResist <= Table[5][1] )
      {
        Serial.print(Table[5][0]);
        Serial.println(" C");
      }
      if(ThermResist >= Table[9][1] && ThermResist <= Table[7][1] )
      {
        Serial.print(Table[9][0]);
        Serial.println(" C");
      }
   } 
  }

}

Output of the code(still loops twice as much): -

Value = 685.00	 Voltage = 3.35 V	 Resistance = 4927.11 Ohm
Value = 688.00	 Voltage = 3.36 V	 Resistance = 4862.12 Ohm
Value = 693.00	 Voltage = 3.39 V	 Resistance = 4755.04 Ohm
Value = 695.00	 Voltage = 3.40 V	 Resistance = 4712.64 Ohm
Value = 698.00	 Voltage = 3.41 V	 Resistance = 4649.50 Ohm
Value = 701.00	 Voltage = 3.43 V	 Resistance = 4586.89 Ohm
Value = 704.00	 Voltage = 3.44 V	 Resistance = 4524.82 Ohm
Value = 707.00	 Voltage = 3.46 V	 Resistance = 4463.28 Ohm
25 C
25 C
25 C
25 C
25 C
25 C
25 C
Value = 729.00	 Voltage = 3.56 V	 Resistance = 4027.40 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 731.00	 Voltage = 3.57 V	 Resistance = 3989.07 Ohm
Value = 731.00	 Voltage = 3.57 V	 Resistance = 3989.07 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 733.00	 Voltage = 3.58 V	 Resistance = 3950.95 Ohm
Value = 739.00	 Voltage = 3.61 V	 Resistance = 3837.84 Ohm
Value = 737.00	 Voltage = 3.60 V	 Resistance = 3875.34 Ohm
Value = 739.00	 Voltage = 3.61 V	 Resistance = 3837.84 Ohm
Value = 740.00	 Voltage = 3.62 V	 Resistance = 3819.16 Ohm
Value = 740.00	 Voltage = 3.62 V	 Resistance = 3819.16 Ohm
Value = 742.00	 Voltage = 3.63 V	 Resistance = 3781.96 Ohm
28 C
28 C
28 C
28 C
28 C
28 C
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 756.00	 Voltage = 3.70 V	 Resistance = 3527.08 Ohm
Value = 768.00	 Voltage = 3.75 V	 Resistance = 3316.00 Ohm
Value = 768.00	 Voltage = 3.75 V	 Resistance = 3316.00 Ohm
Value = 769.00	 Voltage = 3.76 V	 Resistance = 3298.70 Ohm
Value = 771.00	 Voltage = 3.77 V	 Resistance = 3264.25 Ohm
30 C
30 C
30 C
30 C
30 C
30 C
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 777.00	 Voltage = 3.80 V	 Resistance = 3161.95 Ohm
34 C
34 C
34 C
34 C
34 C
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 775.00	 Voltage = 3.79 V	 Resistance = 3195.88 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm
Value = 776.00	 Voltage = 3.79 V	 Resistance = 3178.89 Ohm

PaulS:
Changing code that you posted a while ago is a dirty trick. Please do not do that again. Post the code in a new reply.

I've changed the code on reply #44 back to where it was. So don't worry about it. Although, i am still concerned on using the for loop

You've fixed the double index increment issue, but you still pointlessly use two nested for()s just to print the same thing multiple times. And you were the first to recognize you didn't understand why you needed those cycles. Can't you see the indices don't play any role in the code of the inner for() block ?

You either use multiple copy-n-pasted if()s, or you use just one and loop through the table's row. (Hint: you need just one loop).

tuxduino:
You've fixed the double index increment issue, but you still pointlessly use two nested for()s just to print the same thing multiple times. And you were the first to recognize you didn't understand why you needed those cycles. Can't you see the indices don't play any role in the code of the inner for() block ?

You either use multiple copy-n-pasted if()s, or you use just one and loop through the table's row. (Hint: you need just one loop).

The indices doesn't do anything on my for loop. Which is why i don't understand why i need a for loop for this task. It would have made more sense if i knew how to use a one loop routine for my for loop.

      if(ThermResist >= Table[2][1] && ThermResist <= Table[0][1] )
      if(ThermResist >= Table[4][1] && ThermResist <= Table[3][1] )
      if(ThermResist >= Table[6][1] && ThermResist <= Table[5][1] )
      if(ThermResist >= Table[9][1] && ThermResist <= Table[7][1] )

Do you not see a problem with the 1st index in these statements?

The first statement should be comparing ThermResist to Table[1][1] and Table[0][1].
The next one should be, using your incorrect logic, comparing it to Table[3][1] and Table[2][1].

But, what happens if the value is between Table[2][1] and Table[1][1]?

The portion of the table to check, using just ONE if statement is Table[i+1][1] and Table[ i ][1].

Why is the code to check the row inside a loop to check by column? Nothing depends on row. You only need one for loop, for the rows.

You "need" a for loop to scan the table rows, so you don't have to copy-n-paste if() blocks changing only the row index.

(edit: didn't see PaulS's reply before posting :stuck_out_tongue: )

Now you have working code could you tell us why you used this method? Extra marks may be scored for pointing out the problems with this method and a better solution.

Mark

tuxduino:
You "need" a for loop to scan the table rows, so you don't have to copy-n-paste if() blocks changing only the row index.

(edit: didn't see PaulS's reply before posting :stuck_out_tongue: )

I am about to pull my hair out...using a for loop is a complete disaster...whats the point of using this method: -

  for(int i; i<rows; i++)
  {
   if (Table[i+1][1] && Table[i][1])
   {
     Serial.println(Table[i][0]);
     delay(100);
   }
  }

Output:

Value = 647.00	 Voltage = 3.16 V	 Resistance = 5802.47 Ohm
25
26
27
28
29
30
31
32
33
Value = 647.00	 Voltage = 3.16 V	 Resistance = 5802.47 Ohm
25
26
27
28
29
30
31
32
33
Value = 648.00	 Voltage = 3.17 V	 Resistance = 5778.12 Ohm