Go Down

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

Jonraptor

I've been trying to make a 2D array table based on the temperature, and Resistance. But it doesn't seem to work well. Here is the code and the task requirement so forth: -

Complete and test a program that uses a simple lookup table to print the
temperature from a thermistor to the serial console. Test it by holding the
thermistor between two fingers and watch the resistance drop, voltage increase
and temperature increase.

Code: -
Code: [Select]

const int AnalogInPin = A0;

void setup()
{
  Serial.begin(9600); 
}

void loop()
{
int SensorValue = 0;
const int cols = 2;
const int rows = 10;
int DArray [rows] [cols] = {
                            {25, 4470},
                            {26, 4000},
                            {27, 3900},
                            {28, 3800},
                            {29, 3530},
                            {30, 3270},
                            {31, 3170},
                            {32, 3110},
                            {33, 3090},
                            {34, 3070},
                           }; 
int i;                           
float R2 = 10000.0;
float R1;
float Vin = 5.0;
float Vout;
float ThermResist;
   SensorValue = analogRead(AnalogInPin);   
for( i = 0; i <rows; i++)
{
if(ThermResist == DArray[i][2])
   Serial.println(DArray[1][0]);
}

//  Serial.println(DArray[rows] [cols]);

  Serial.print("Voltage: ");
  Vout = (((SensorValue+1)*5)/1024.0);
  Serial.print(Vout);
  Serial.print(" V");

  Serial.print("\t Resistance: ");
  ThermResist = ((R2 * Vin)/Vout) - R2; 
  Serial.print(ThermResist, DArray[i][2]);
  Serial.println(" Ohm ");
}



Thank you :)

Jonraptor

In addition to that, i am having trouble converting the Temperature from the 2D array to Kelvins, Celsius, and Fahrenheit.

If someone would be able to explain how am i suppose to solve this task without adding the SteinHart-Hart equation (just a simple way to convert the default temperature from the 2D array to K, C & F)

Improve the structure of your program by creating, implementing and using
the following functions:
int getTemperatureUsingLookup(int pin); //gets the temperature from a
thermistor attached to an analog pin
int getTemperatureInKelvin(int pin); //returns the temperature in K.
int getTemperatureInCelcius(int pin); //returns the temperature in C.
int getTemperatureInFarenheit(int pin); //returns the temperature in F

dxw00d


Nick Gammon


In addition to that, i am having trouble converting the Temperature from the 2D array to Kelvins, Celsius, and Fahrenheit.


Converting the figure in the table, in Celsius, to Celsius would be quite taxing. It's a complex problem, for sure.

Jonraptor

#4
Sep 30, 2012, 09:54 am Last Edit: Sep 30, 2012, 11:03 am by Jonraptor Reason: 1

When is our homework assignment due in?


I dunno, haven't checked tbh

westfw

While there's a lot of things I don't like about your sketch, and I'm not at all sure you're doing what the assignment expects, I think the reason that it's not working at all is here:
Code: [Select]
float ThermResist;
   SensorValue = analogRead(AnalogInPin);   
for( i = 0; i <rows; i++)
{
if(ThermResist == DArray[i][2])

You read a sensorvalue, and then go looking for it in your table for ThermResist (which was never initialized.)
As you have it structured now, you should be looking for SensorValue in your array, shouldn't you?  Also, since you have multiple values of SensorValue that should map to a particular temperature, you probably don't want to use "==" in your comparison.

If two-dimensional arrays are confusing you, you could use two one-dimensional arrays (one holding the sensorvalues, and the other holding the temperatures)

Jonraptor


While there's a lot of things I don't like about your sketch, and I'm not at all sure you're doing what the assignment expects, I think the reason that it's not working at all is here:
Code: [Select]
float ThermResist;
   SensorValue = analogRead(AnalogInPin);   
for( i = 0; i <rows; i++)
{
if(ThermResist == DArray[i][2])

You read a sensorvalue, and then go looking for it in your table for ThermResist (which was never initialized.)
As you have it structured now, you should be looking for SensorValue in your array, shouldn't you?  Also, since you have multiple values of SensorValue that should map to a particular temperature, you probably don't want to use "==" in your comparison.

If two-dimensional arrays are confusing you, you could use two one-dimensional arrays (one holding the sensorvalues, and the other holding the temperatures)



Here is the lookup table that i was referring to before: -

Temp                   Resist
25                         4470
28                         3800
30                         3270
34                         3070

tuxduino

I think you should calculate ThermResist from SensorValue before using it :P

Also, try this sketch first:
Code: [Select]

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println(analogRead(0));
    delay(500);
}

and you should see why searching for an exact value like you seem to be doing in your sketch is a bad idea.

My suggestion is to either compare the analog read or calculated resistance against ranges or use a formula instead of a lookup table.

my 2 cents

Jonraptor

#8
Oct 04, 2012, 11:47 am Last Edit: Oct 04, 2012, 11:52 am by Jonraptor Reason: 1
Ok Everyone, i've done some hard work trying to figure out the problem. Everything seems to be working fine. Except the part in the for loop won't be able to print out the statement if it goes less than what i have provided on my 2D Array Table. Here is my current code that i have modified: -

Code: [Select]

const int ThermPin = A0;
const int rows = 10;
const int cols = 2;
int Table[rows][cols] ={
                           { 25, 4470 },
                           { 26, 4250 },
                           { 27, 4030 },
                           { 28, 3800 },
                           { 29, 3530 },
                           { 30, 3270 },
                           { 31, 3200 },
                           { 32, 3170 },
                           { 33, 3100 },
                           { 34, 3070 }
                      };    
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;
                       
/*
   Keeps on looping for times everytime i change the if statement to '<=' instead of '==' (e.g. ThermResist <= Table[4][1] provides a printout statement if the thermresist is less than of what it have been provided
  in the table. But it doesn't do that, instead it loops the printout result 4 times and then prints out the sensorval, volt, and Ohms.Then it keeps on looping the same output, which i couldn't find an alternative to  
  avoid this for loop from looping the printout 4 times, as well as triggering the function when it hits below table[4][1]).
*/
for(int i=0; i<rows; i++)
{
 for(int j=0; j<cols; j++)
 {
     if(ThermResist == Table[4][1])
     {
       Serial.print(Table[4][0]);
       Serial.println(" C");
     }
   j++;
 }
 i++;
}




 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");
}

AWOL

Code: [Select]
float ThermResist = 0.0;
...
...
      if(ThermResist == Table[4][1])

Nothing has changed, it seems to me.
"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


Code: [Select]
float ThermResist = 0.0;
...
...
      if(ThermResist == Table[4][1])

Nothing has changed, it seems to me.


Yeah, it doesn't printout the value if the thermresist went less than of what have been provided on Table[4][1]. Except, if i change the value to either <= or >=. It provides a constant loop of the printout based on where the output is located in the table (e.g. Table[4][1] will trigger a printout of Table[4][0] 4 times, and then prints out the sensorval, volts, ohms, and then keeps on rinse and repeating the same process over and over again.

AWOL

ThermResist always has a value of zero.
What were you expecting to happen?
"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


ThermResist always has a value of zero.
What were you expecting to happen?

Mm....hold on a min. I think i might have an alternative to that situation.

Jonraptor


ThermResist always has a value of zero.
What were you expecting to happen?

Ok, i've tried to move the printout code for the sensorval, volt, and Ohm before the for loop is even initiated. But, nothing happens:

Code: [Select]

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[4][1])
      {
        Serial.print(Table[4][0]);
        Serial.println(" C");
      }
    j++;
  }
  i++;
}

AWOL

#14
Oct 04, 2012, 12:07 pm Last Edit: Oct 04, 2012, 12:09 pm by AWOL Reason: 1
Code: [Select]
if(ThermResist == Table[4][1]) ?

In fact, even if you fix the subscripts, skipping over every other table entry isn't going to help.
Lose the excess subscript increments.
"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.

Go Up