Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35491
Seattle, WA USA
|
 |
« Reply #60 on: October 04, 2012, 10:31:13 am » |
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.
|
|
|
|
« Last Edit: October 04, 2012, 10:33:36 am by PaulS »
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #61 on: October 04, 2012, 10:32:48 am » |
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  )
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 671
|
 |
« Reply #62 on: October 04, 2012, 10:35:40 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #63 on: October 04, 2012, 10:47:47 am » |
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: - 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
|
|
|
|
« Last Edit: October 04, 2012, 10:56:53 am by Jonraptor »
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #64 on: October 04, 2012, 10:49:05 am » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 671
|
 |
« Reply #65 on: October 04, 2012, 11:06:35 am » |
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 if (sensorValue >= lowest && sensorValue =< highest ){ // your code to translate the sensorValue } else { Serial.print("Error or something"); }
Mark
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35491
Seattle, WA USA
|
 |
« Reply #66 on: October 04, 2012, 11:10:46 am » |
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: for(int i; i<rows; i++) { if(ThermResist >= Table[i+1][1] && ThermResist <= Table[i][1] ) { Serial.println(Table[i][0]); delay(100); } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #67 on: October 04, 2012, 11:11:34 am » |
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  : - 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");
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #68 on: October 04, 2012, 11:16:08 am » |
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: 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). 
|
|
|
|
« Last Edit: October 04, 2012, 11:17:48 am by Jonraptor »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35491
Seattle, WA USA
|
 |
« Reply #69 on: October 04, 2012, 11:17:00 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #70 on: October 04, 2012, 11:21:37 am » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35491
Seattle, WA USA
|
 |
« Reply #71 on: October 04, 2012, 11:22:28 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #72 on: October 04, 2012, 11:25:43 am » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #73 on: October 04, 2012, 11:30:04 am » |
My latest update for 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; i<rows; i++) { if(ThermResist >= Table[i+1][1] && ThermResist <= Table[i][1] ) { Serial.print(Table[i][0]); Serial.print("\t"); } } }
Output: - Temperature = 25 C Value = 709.00 Voltage = 3.47 V Resistance = 4422.54 Ohm Temperature = 25 C Value = 712.00 Voltage = 3.48 V Resistance = 4361.85 Ohm Temperature = 25 C Value = 712.00 Voltage = 3.48 V Resistance = 4361.85 Ohm Temperature = 25 C Value = 717.00 Voltage = 3.51 V Resistance = 4261.84 Ohm Temperature = 25 C Value = 716.00 Voltage = 3.50 V Resistance = 4281.73 Ohm Temperature = 25 C Value = 719.00 Voltage = 3.52 V Resistance = 4222.22 Ohm Temperature = 26 C Value = 719.00 Voltage = 3.52 V Resistance = 4222.22 Ohm Temperature = 26 C Value = 723.00 Voltage = 3.54 V Resistance = 4143.65 Ohm Temperature = 26 C Value = 724.00 Voltage = 3.54 V Resistance = 4124.14 Ohm Temperature = 26 C Value = 725.00 Voltage = 3.54 V Resistance = 4104.68 Ohm Temperature = 26 C Value = 726.00 Voltage = 3.55 V Resistance = 4085.28 Ohm Temperature = 26 C Value = 728.00 Voltage = 3.56 V Resistance = 4046.64 Ohm Temperature = 26 C Value = 729.00 Voltage = 3.56 V Resistance = 4027.40 Ohm Temperature = 27 C Value = 730.00 Voltage = 3.57 V Resistance = 4008.21 Ohm Temperature = 27 C Value = 732.00 Voltage = 3.58 V Resistance = 3969.99 Ohm Temperature = 27 C Value = 733.00 Voltage = 3.58 V Resistance = 3950.95 Ohm Temperature = 27 C Value = 734.00 Voltage = 3.59 V Resistance = 3931.97 Ohm Temperature = 27 C Value = 735.00 Voltage = 3.59 V Resistance = 3913.04 Ohm Temperature = 27 C Value = 734.00 Voltage = 3.59 V Resistance = 3931.97 Ohm Temperature = 27 C Value = 737.00 Voltage = 3.60 V Resistance = 3875.34 Ohm Temperature = 27 C Value = 737.00 Voltage = 3.60 V Resistance = 3875.34 Ohm Temperature = 27 C Value = 735.00 Voltage = 3.59 V Resistance = 3913.04 Ohm Temperature = 27 C Value = 739.00 Voltage = 3.61 V Resistance = 3837.84 Ohm Temperature = 27 C Value = 739.00 Voltage = 3.61 V Resistance = 3837.84 Ohm Temperature = 27 C Value = 738.00 Voltage = 3.61 V Resistance = 3856.56 Ohm Temperature = 27 C Value = 740.00 Voltage = 3.62 V Resistance = 3819.16 Ohm Temperature = 27 C Value = 740.00 Voltage = 3.62 V Resistance = 3819.16 Ohm Temperature = 27 C Value = 741.00 Voltage = 3.62 V Resistance = 3800.54 Ohm Temperature = 27 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 Ohm Temperature = 28 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 Ohm Temperature = 28 C Value = 743.00 Voltage = 3.63 V Resistance = 3763.44 Ohm Temperature = 28 C Value = 743.00 Voltage = 3.63 V Resistance = 3763.44 Ohm Temperature = 28 C Value = 742.00 Voltage = 3.63 V Resistance = 3781.96 Ohm Temperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 Ohm Temperature = 28 C Value = 744.00 Voltage = 3.64 V Resistance = 3744.97 Ohm Temperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 Ohm Temperature = 28 C Value = 746.00 Voltage = 3.65 V Resistance = 3708.17 Ohm Temperature = 28 C Value = 746.00 Voltage = 3.65 V Resistance = 3708.17 Ohm Temperature = 28 C Value = 745.00 Voltage = 3.64 V Resistance = 3726.54 Ohm Temperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 Ohm Temperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 Ohm Temperature = 29 C Value = 757.00 Voltage = 3.70 V Resistance = 3509.23 Ohm Temperature = 29 C Value = 757.00 Voltage = 3.70 V Resistance = 3509.23 Ohm Temperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 Ohm Temperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 Ohm Temperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 Ohm Temperature = 29 C Value = 759.00 Voltage = 3.71 V Resistance = 3473.68 Ohm Temperature = 29 C Value = 758.00 Voltage = 3.71 V Resistance = 3491.44 Ohm Temperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 Ohm Temperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 Ohm Temperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 Ohm Temperature = 29 C Value = 760.00 Voltage = 3.72 V Resistance = 3455.98 Ohm Temperature = 30 C Value = 769.00 Voltage = 3.76 V Resistance = 3298.70 Ohm Temperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 770.00 Voltage = 3.76 V Resistance = 3281.45 Ohm Temperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 770.00 Voltage = 3.76 V Resistance = 3281.45 Ohm Temperature = 29 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 30 C Value = 774.00 Voltage = 3.78 V Resistance = 3212.90 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 772.00 Voltage = 3.77 V Resistance = 3247.09 Ohm Temperature = 30 C Value = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm Temperature = 30 C Value = 773.00 Voltage = 3.78 V Resistance = 3229.97 Ohm Temperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 Ohm Temperature = 31 C Value = 776.00 Voltage = 3.79 V Resistance = 3178.89 Ohm Temperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 Ohm Temperature = 31 C Value = 775.00 Voltage = 3.79 V Resistance = 3195.88 Ohm Temperature = 32 C Value = 780.00 Voltage = 3.81 V Resistance = 3111.40 Ohm Temperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 Ohm Temperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 Ohm Temperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 Ohm Temperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 Ohm Temperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 Ohm Temperature = 32 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 Ohm Temperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 Ohm Temperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 Ohm Temperature = 32 C Value = 777.00 Voltage = 3.80 V Resistance = 3161.95 Ohm Temperature = 32 C Value = 778.00 Voltage = 3.80 V Resistance = 3145.06 Ohm Temperature = 32 C Value = 780.00 Voltage = 3.81 V Resistance = 3111.40 Ohm Temperature = 32 C Value = 782.00 Voltage = 3.82 V Resistance = 3077.91 Ohm Temperature = 33 C Value = 779.00 Voltage = 3.81 V Resistance = 3128.20 Ohm Temperature = 34 C Value = 783.00 Voltage = 3.83 V Resistance = 3061.22 Ohm Temperature = 34 C Value = 783.00 Voltage = 3.83 V Resistance = 3061.22 Ohm Temperature = 34 C Value = 782.00 Voltage = 3.82 V Resistance = 3077.91 Ohm
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #74 on: October 04, 2012, 11:31:54 am » |
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  .
|
|
|
|
|
Logged
|
|
|
|
|
|