Go Down

### Topic: Having trouble on solving the following tasks (Read 11094 times)previous topic - next topic

#### PaulS

#60
##### Oct 04, 2012, 05:31 pmLast Edit: Oct 04, 2012, 05:33 pm by PaulS Reason: 1
Code: [Select]
`      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.

#### marcello.romani

#61
##### Oct 04, 2012, 05:32 pm
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 )

#### holmes4

#62
##### Oct 04, 2012, 05:35 pm
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

#### Jonraptor

#63
##### Oct 04, 2012, 05:47 pmLast Edit: Oct 04, 2012, 05:56 pm by Jonraptor Reason: 1

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 )

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

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

Code: [Select]
`Value = 647.00 Voltage = 3.16 V Resistance = 5802.47 Ohm252627282930313233Value = 647.00 Voltage = 3.16 V Resistance = 5802.47 Ohm252627282930313233Value = 648.00 Voltage = 3.17 V Resistance = 5778.12 Ohm`

#### Jonraptor

#64
##### Oct 04, 2012, 05:49 pm

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

Umm, i am still working on it...trying to figure out how to use a for loop for this part...

#### holmes4

#65
##### Oct 04, 2012, 06:06 pm
Looking at the the last full version of your code that you posted you are missing a range check on sensorValue before you use it. The sensor can report a much greater range than your table deals with.

Something like
Code: [Select]
`if (sensorValue >= lowest && sensorValue =< highest ){// your code to translate the sensorValue} else {Serial.print("Error or something");}`

Mark

#### PaulS

#66
##### Oct 04, 2012, 06:10 pm
Code: [Select]
`  for(int i; i<rows; i++)  {   if (Table[i+1][1] && Table[i][1])   {     Serial.println(Table[i][0]);     delay(100);   }  } `
The if statement in this loop does not look anything like the (correct) if statements you were using before. Try this:
Code: [Select]
`  for(int i; i<rows; i++)  {   if(ThermResist >= Table[i+1][1] && ThermResist <= Table[i][1] )   {     Serial.println(Table[i][0]);     delay(100);   }  } `

#### Jonraptor

#67
##### Oct 04, 2012, 06:11 pm

Code: [Select]
`      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].

You mean something like this  =(: -
Code: [Select]
`if(ThermResist >= Table[1][1] && ThermResist <= Table[0][1] )        Serial.print(Table[0][0]);        Serial.println(" C");     if(ThermResist >= Table[2][1] && ThermResist <= Table[1][1] )        Serial.print(Table[1][0]);        Serial.println(" C");     if(ThermResist >= Table[3][1] && ThermResist <= Table[2][1] )        Serial.print(Table[2][0]);        Serial.println(" C");     if(ThermResist >= Table[4][1] && ThermResist <= Table[3][1] )        Serial.print(Table[3][0]);        Serial.println(" C");     if(ThermResist >= Table[5][1] && ThermResist <= Table[4][1] )        Serial.print(Table[4][0]);        Serial.println(" C");     if(ThermResist >= Table[6][1] && ThermResist <= Table[5][1] )        Serial.print(Table[5][0]);        Serial.println(" C");     if(ThermResist >= Table[7][1] && ThermResist <= Table[6][1] )        Serial.print(Table[6][0]);        Serial.println(" C");     if(ThermResist >= Table[8][1] && ThermResist <= Table[7][1] )        Serial.print(Table[7][0]);        Serial.println(" C");     if(ThermResist >= Table[9][1] && ThermResist <= Table[8][1] )        Serial.print(Table[8][0]);        Serial.println(" C");     if(ThermResist >= Table[9][1] && ThermResist <= Table[9][1] )        Serial.print(Table[9][0]);        Serial.println(" C");       `

#### Jonraptor

#68
##### Oct 04, 2012, 06:16 pmLast Edit: Oct 04, 2012, 06:17 pm by Jonraptor Reason: 1

Code: [Select]
`  for(int i; i<rows; i++)  {   if (Table[i+1][1] && Table[i][1])   {     Serial.println(Table[i][0]);     delay(100);   }  } `
The if statement in this loop does not look anything like the (correct) if statements you were using before. Try this:
Code: [Select]
`  for(int i; i<rows; i++)  {   if(ThermResist >= Table[i+1][1] && ThermResist <= Table[i][1] )   {     Serial.println(Table[i][0]);     delay(100);   }  } `

You got me...i am nothing more than an idiot when coming to using for loops....i haven't thought about using that if statement up and until you posted a reply about it....and it worked...May i ask, do you happen to know where to find for loop examples i could use to practice for arduino? because i really need to fully grasp the concept in my head, i don't want to just type it or write in something without knowing what it does(which you have explained in the previous replies, and i've understood the concept theoritically, but applying it in a programming statement was hard for me to implement).  =(

#### PaulS

#69
##### Oct 04, 2012, 06:17 pm
Quote
You mean something like this

Yes. And, now it should be obvious that a for loop can be used to step through the first index.

#### Jonraptor

#70
##### Oct 04, 2012, 06:21 pm

Quote
You mean something like this

Yes. And, now it should be obvious that a for loop can be used to step through the first index.

Speaking of a for loop, i can see how useful the for loop provides for the whole concept of programming. Not only it saves code space, but it also helps out on providing the right amount of values that you need to printout a statement that is accurate.

But the problem on using a for loop is, it is hard for some people to fully master using a for loop....and that can only applied through practice. Which is why i need to know where to find more examples i could use to boost my experience on using a for loop

#### PaulS

#71
##### Oct 04, 2012, 06:22 pm
Quote
May i ask, do you happen to know where to find for loop examples i could use to practice for arduino?

I think that you are confusing what happens in the body of the loop with the for statement itself.

A for loop, as you can see from your examples, just says "Do this until some condition is met, starting with these initial conditions, and doing that after each iteration".

What happens inside the loop is as simple or as complex as you can make it.

The key is knowing how/where to use the loop index inside the loop, and how not to use it. Generally, modifying the loop index inside the loop requires extreme care.

#### Jonraptor

#72
##### Oct 04, 2012, 06:25 pm

Quote
May i ask, do you happen to know where to find for loop examples i could use to practice for arduino?

I think that you are confusing what happens in the body of the loop with the for statement itself.

A for loop, as you can see from your examples, just says "Do this until some condition is met, starting with these initial conditions, and doing that after each iteration".

What happens inside the loop is as simple or as complex as you can make it.

The key is knowing how/where to use the loop index inside the loop, and how not to use it. Generally, modifying the loop index inside the loop requires extreme care.

Yeah, i can see that...based on my previous attempts on using a for loop....till now, it seems all clear to me...I'll give you my latest update for my code and output in a min

#### Jonraptor

#73
##### Oct 04, 2012, 06:30 pm
My latest update for my code : -

Code: [Select]
`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; i<rows; i++)  {   if(ThermResist >= Table[i+1][1] && ThermResist <= Table[i][1] )   {     Serial.print(Table[i][0]);     Serial.print("\t");   }  } }`

Output: -
Code: [Select]
`Temperature = 25 C Value = 709.00 Voltage = 3.47 V Resistance = 4422.54 OhmTemperature = 25 C Value = 712.00 Voltage = 3.48 V Resistance = 4361.85 OhmTemperature = 25 C Value = 712.00 Voltage = 3.48 V Resistance = 4361.85 OhmTemperature = 25 C Value = 717.00 Voltage = 3.51 V Resistance = 4261.84 OhmTemperature = 25 C Value = 716.00 Voltage = 3.50 V Resistance = 4281.73 OhmTemperature = 25 C Value = 719.00 Voltage = 3.52 V Resistance = 4222.22 OhmTemperature = 26 C Value = 719.00 Voltage = 3.52 V Resistance = 4222.22 OhmTemperature = 26 C Value = 723.00 Voltage = 3.54 V Resistance = 4143.65 OhmTemperature = 26 C Value = 724.00 Voltage = 3.54 V Resistance = 4124.14 OhmTemperature = 26 C Value = 725.00 Voltage = 3.54 V Resistance = 4104.68 OhmTemperature = 26 C Value = 726.00 Voltage = 3.55 V Resistance = 4085.28 OhmTemperature = 26 C Value = 728.00 Voltage = 3.56 V Resistance = 4046.64 OhmTemperature = 26 C Value = 729.00 Voltage = 3.56 V Resistance = 4027.40 OhmTemperature = 27 C Value = 730.00 Voltage = 3.57 V Resistance = 4008.21 OhmTemperature = 27 C Value = 732.00 Voltage = 3.58 V Resistance = 3969.99 OhmTemperature = 27 C Value = 733.00 Voltage = 3.58 V Resistance = 3950.95 OhmTemperature = 27 C Value = 734.00 Voltage = 3.59 V Resistance = 3931.97 OhmTemperature = 27 C Value = 735.00 Voltage = 3.59 V Resistance = 3913.04 OhmTemperature = 27 C Value = 734.00 Voltage = 3.59 V Resistance = 3931.97 OhmTemperature = 27 C Value = 737.00 Voltage = 3.60 V Resistance = 3875.34 OhmTemperature = 27 C Value = 737.00 Voltage = 3.60 V Resistance = 3875.34 OhmTemperature = 27 C Value = 735.00 Voltage = 3.59 V Resistance = 3913.04 OhmTemperature = 27 C Value = 739.00 Voltage = 3.61 V Resistance = 3837.84 OhmTemperature = 27 C Value = 739.00 Voltage = 3.61 V Resistance = 3837.84 OhmTemperature = 27 C Value = 738.00 Voltage = 3.61 V Resistance = 3856.56 OhmTemperature = 27 C Value = 740.00 Voltage = 3.62 V Resistance = 3819.16 OhmTemperature = 27 C Value = 740.00 Voltage = 3.62 V Resistance = 3819.16 OhmTemperature = 27 C Value = 741.00 Voltage = 3.62 V Resistance = 3800.54 OhmTemperature = 27 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 OhmTemperature = 28 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 OhmTemperature = 28 C Value = 743.00 Voltage = 3.63 V Resistance = 3763.44 OhmTemperature = 28 C Value = 743.00 Voltage = 3.63 V Resistance = 3763.44 OhmTemperature = 28 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 OhmTemperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 OhmTemperature = 28 C Value = 744.00 Voltage = 3.64 V Resistance = 3744.97 OhmTemperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 OhmTemperature = 28 C Value = 746.00 Voltage = 3.65 V Resistance = 3708.17 OhmTemperature = 28 C Value = 746.00 Voltage = 3.65 V Resistance = 3708.17 OhmTemperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 OhmTemperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 OhmTemperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 OhmTemperature = 29 C Value = 757.00 Voltage = 3.70 V Resistance = 3509.23 OhmTemperature = 29 C Value = 757.00 Voltage = 3.70 V Resistance = 3509.23 OhmTemperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 OhmTemperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 OhmTemperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 OhmTemperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 OhmTemperature = 29 C Value = 758.00 Voltage = 3.71 V Resistance = 3491.44 OhmTemperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 OhmTemperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 OhmTemperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 OhmTemperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 OhmTemperature = 30 C Value = 769.00 Voltage = 3.76 V Resistance = 3298.70 OhmTemperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 770.00 Voltage = 3.76 V Resistance = 3281.45 OhmTemperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 770.00 Voltage = 3.76 V Resistance = 3281.45 OhmTemperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 30 C Value = 774.00 Voltage = 3.78 V Resistance = 3212.90 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 OhmTemperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 OhmTemperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 OhmTemperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmTemperature = 31 C Value = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmTemperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmTemperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmTemperature = 32 C Value = 780.00 Voltage = 3.81 V Resistance = 3111.40 OhmTemperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 OhmTemperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 OhmTemperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 OhmTemperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 OhmTemperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 OhmTemperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 OhmTemperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 OhmTemperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 OhmTemperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 OhmTemperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 OhmTemperature = 32 C Value = 780.00 Voltage = 3.81 V Resistance = 3111.40 OhmTemperature = 32 C Value = 782.00 Voltage = 3.82 V Resistance = 3077.91 OhmTemperature = 33 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 OhmTemperature = 34 C Value = 783.00 Voltage = 3.83 V Resistance = 3061.22 OhmTemperature = 34 C Value = 783.00 Voltage = 3.83 V Resistance = 3061.22 OhmTemperature = 34 C Value = 782.00 Voltage = 3.82 V Resistance = 3077.91 Ohm`

#### Jonraptor

#74
##### Oct 04, 2012, 06:31 pm
I think i need to be careful the next time i use a for loop. Thanks for the great help guys. I'll be sure to let you know what i've done for my next task .

Go Up