Go Down

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

#### Jonraptor

#30
##### Oct 04, 2012, 02:29 pmLast Edit: Oct 04, 2012, 03:02 pm by Jonraptor Reason: 1

Quote
Lose the excess subscript increments. How so? The task mentions that i need to make a simple lookout table based on 2D array (without using the steinhart-hart equation).

Ok, I'll repeat: you get an analog value and you search it with equality among a (small) set of predefined values. An analog value in Arduino is an integer number between 0 and 1023. If you compare it to, say, 10 values you roughly have 1% probability to "catch" it each time.

Back to your code, it's clear that you're trying to identify the type of battery based on its weight. Each battery type has a minimum and maximum weight. You want to determine in which weight range your analog value falls. For this you need to test it against the minimum and maximum value of each range. And for this you need >= and <=, not ==.

I don't understand why using a lookup table implies using (only) ==.

If you insist on using ==, you could probably make a 1024-elements lookup table and use the analogRead result as an index into it. Total waste of RAM, IMHO, but using just 1 byte per element it could be doable (you'd be using half RAM just for that...).

(edit: sorry I assumed you would search using an integer value. If you use floats, then == is _definitely out of question_)

I think i pointed out that i've removed the for loop in replacement for adding an if statement based on the range between values.

#### tuxduino

#31
##### Oct 04, 2012, 02:32 pm
Quote
I think i pointed out that i've removed the for loop in replacement for adding and if statement based on the range between values.

I'm sorry, I wrote that comment before seeing your last code.

#### AWOL

#32
##### Oct 04, 2012, 02:34 pm
Quote
Value = 756.00    Voltage = 3.70 V    Resistance = 3527.08 Ohm
29 C

What's the problem?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Jonraptor

#33
##### Oct 04, 2012, 03:00 pmLast Edit: Oct 04, 2012, 03:04 pm by Jonraptor Reason: 1

Quote
Value = 756.00    Voltage = 3.70 V    Resistance = 3527.08 Ohm
29 C

What's the problem?

I'll point out the issue in this.

Here is the code that i've implemented for my if statement: -

Code: [Select]
`      if(ThermResist >= Table[5][1] &&ThermResist <= Table[4][1] )      {        Serial.print(Table[4][0]);         Serial.println(" C");      }`

And here is the output of the function trigger less than 3530: -
Code: [Select]
`Value = 754.00 Voltage = 3.69 V Resistance = 3562.91 OhmValue = 755.00 Voltage = 3.69 V Resistance = 3544.97 OhmValue = 755.00 Voltage = 3.69 V Resistance = 3544.97 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 Ohm29 CValue = 754.00 Voltage = 3.69 V Resistance = 3562.91 OhmValue = 753.00 Voltage = 3.68 V Resistance = 3580.90 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 Ohm29 CValue = 754.00 Voltage = 3.69 V Resistance = 3562.91 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 Ohm29 C`

I am  simply trying to print out the statement that is way out of the range which i am trying to implement (between 3800 & 3530). Instead, i get the printout below 3530.. which for me, is a catastrophe..

#### AWOL

#34
##### Oct 04, 2012, 03:10 pm
I'm sorry, I really don't see the problem.

3527.08 is greater than 3270 AND it is less than 3530.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Jonraptor

#35
##### Oct 04, 2012, 03:14 pm

I'm sorry, I really don't see the problem.

3527.08 is greater than 3270 AND it is less than 3530.

Code: [Select]
`  if(ThermResist >= Table[5][1](3530) &&ThermResist <= Table[4][1](3800) )      {        Serial.print(Table[4][0]);         Serial.println(" C");      }`

#### AWOL

#36
##### Oct 04, 2012, 03:15 pm
Code: [Select]
`f(ThermResist >= Table[5][1](3270) &&ThermResist <= Table[4][1](3530) )`
is what you meant to type, I guess.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Jonraptor

#37
##### Oct 04, 2012, 03:25 pmLast Edit: Oct 04, 2012, 03:31 pm by Jonraptor Reason: 1

Code: [Select]
`f(ThermResist >= Table[5][1](3270) &&ThermResist <= Table[4][1](3530) )`
is what you meant to type, I guess.

OMG, i have completely forgotten that the array starts with 0, rather than 1. My apologies for the misunderstanding, ill get this sort out ASAP and show you the output .

#### Jonraptor

#38
##### Oct 04, 2012, 03:31 pm
I've solved the problem by counting the array from 0-9, turned out i have mistaken 4 with 3530, and 5 with 3270.

Here the code that i've altered after a while: -

Code: [Select]
`      if(ThermResist >= Table[4][1] && ThermResist <= Table[3][1] )      {        Serial.print(Table[4][0]);         Serial.println(" C");      }`

And here is the output that i am quite happy on (between 3800 && 3530): -
Code: [Select]
`Value = 738.00 Voltage = 3.61 V Resistance = 3856.56 OhmValue = 739.00 Voltage = 3.61 V Resistance = 3837.84 OhmValue = 741.00 Voltage = 3.62 V Resistance = 3800.54 OhmValue = 741.00 Voltage = 3.62 V Resistance = 3800.54 OhmValue = 741.00 Voltage = 3.62 V Resistance = 3800.54 OhmValue = 743.00 Voltage = 3.63 V Resistance = 3763.44 Ohm29 CValue = 741.00 Voltage = 3.62 V Resistance = 3800.54 OhmValue = 743.00 Voltage = 3.63 V Resistance = 3763.44 Ohm29 CValue = 743.00 Voltage = 3.63 V Resistance = 3763.44 Ohm29 CValue = 745.00 Voltage = 3.64 V Resistance = 3726.54 Ohm29 CValue = 742.00 Voltage = 3.63 V Resistance = 3781.96 Ohm29 CValue = 745.00 Voltage = 3.64 V Resistance = 3726.54 Ohm29 C29 CValue = 755.00 Voltage = 3.69 V Resistance = 3544.97 Ohm29 CValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 753.00 Voltage = 3.68 V Resistance = 3580.90 Ohm29 CValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 758.00 Voltage = 3.71 V Resistance = 3491.44 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 758.00 Voltage = 3.71 V Resistance = 3491.44 OhmValue = 757.00 Voltage = 3.70 V Resistance = 3509.23 OhmValue = 757.00 Voltage = 3.70 V Resistance = 3509.23 OhmValue = 757.00 Voltage = 3.70 V Resistance = 3509.23 Ohm`

#### AWOL

#39
##### Oct 04, 2012, 03:33 pm
Time to put your for loops back.

Thanks for sticking with it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Jonraptor

#40
##### Oct 04, 2012, 03:35 pm
All i can say is, thanks a lot for your time and patience for solving this issue with me. I am sorry if i have been harsh trying to solve this problem. But, it's sure is worth to know that the Array usually starts at 0 and ends with 9 if the row is equals to 10, and the column starts with 0 and ends with 1 if the cols is equals to 2.

Thank you a lot for your time and patience with me . There is another task that i am trying to solve as well, but for now. let me get this code organized.

#### Jonraptor

#41
##### Oct 04, 2012, 03:38 pm
Yes, ill put the for loops back once im done with arranging my code. Once im done, ill display what i've altered with the output

#### AWOL

#42
##### Oct 04, 2012, 03:42 pm
The thing to remember here is that the compiled code (when it is that simple and that explicit) rarely lies.
If you see that a result that is being printed when perhaps you don't expect it to be, check and double check the conditions and fit the result to the observation.

I realised as soon as you typed your interpretation of the table values what the problem was, but you found it yourself from my correction, and you gained a valuable lesson about arrays which will stick in your mind better than if I had simply told you.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### PaulS

#43
##### Oct 04, 2012, 03:56 pm
I'm curious why the values in the 2nd column of the lookup table are in decreasing order. I find it easier to understand when a value is in range when the lookup table is in increasing order.

#### Jonraptor

#44
##### Oct 04, 2012, 04:00 pmLast Edit: Oct 04, 2012, 05:18 pm by Jonraptor Reason: 1
Here is my modified version of 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 = 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 !): -
Code: [Select]
`Value = 685.00 Voltage = 3.35 V Resistance = 4927.11 OhmValue = 688.00 Voltage = 3.36 V Resistance = 4862.12 OhmValue = 693.00 Voltage = 3.39 V Resistance = 4755.04 OhmValue = 695.00 Voltage = 3.40 V Resistance = 4712.64 OhmValue = 698.00 Voltage = 3.41 V Resistance = 4649.50 OhmValue = 701.00 Voltage = 3.43 V Resistance = 4586.89 OhmValue = 704.00 Voltage = 3.44 V Resistance = 4524.82 OhmValue = 707.00 Voltage = 3.46 V Resistance = 4463.28 Ohm25 C25 C25 C25 C25 C25 C25 CValue = 729.00 Voltage = 3.56 V Resistance = 4027.40 OhmValue = 733.00 Voltage = 3.58 V Resistance = 3950.95 OhmValue = 731.00 Voltage = 3.57 V Resistance = 3989.07 OhmValue = 731.00 Voltage = 3.57 V Resistance = 3989.07 OhmValue = 733.00 Voltage = 3.58 V Resistance = 3950.95 OhmValue = 733.00 Voltage = 3.58 V Resistance = 3950.95 OhmValue = 739.00 Voltage = 3.61 V Resistance = 3837.84 OhmValue = 737.00 Voltage = 3.60 V Resistance = 3875.34 OhmValue = 739.00 Voltage = 3.61 V Resistance = 3837.84 OhmValue = 740.00 Voltage = 3.62 V Resistance = 3819.16 OhmValue = 740.00 Voltage = 3.62 V Resistance = 3819.16 OhmValue = 742.00 Voltage = 3.63 V Resistance = 3781.96 Ohm28 C28 C28 C28 C28 C28 CValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 756.00 Voltage = 3.70 V Resistance = 3527.08 OhmValue = 768.00 Voltage = 3.75 V Resistance = 3316.00 OhmValue = 768.00 Voltage = 3.75 V Resistance = 3316.00 OhmValue = 769.00 Voltage = 3.76 V Resistance = 3298.70 OhmValue = 771.00 Voltage = 3.77 V Resistance = 3264.25 Ohm30 C30 C30 C30 C30 C30 CValue = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 777.00 Voltage = 3.80 V Resistance = 3161.95 Ohm34 C34 C34 C34 C34 CValue = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmValue = 775.00 Voltage = 3.79 V Resistance = 3195.88 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 OhmValue = 776.00 Voltage = 3.79 V Resistance = 3178.89 Ohm`

Go Up

Please enter a valid email to subscribe