Pages: [1] 2 3 ... 6   Go Down
Author Topic: Having trouble on solving the following tasks  (Read 4520 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When is our homework assignment due in?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When is our homework assignment due in?

I dunno, haven't checked tbh
« Last Edit: September 30, 2012, 04:03:35 am by Jonraptor » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 132
Posts: 6746
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Offline 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you should calculate ThermResist from SensorValue before using it smiley-razz

Also, try this sketch first:
Code:
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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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");
}
« Last Edit: October 04, 2012, 04:52:28 am by Jonraptor » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26220
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float ThermResist = 0.0;
...
...
      if(ThermResist == Table[4][1])
Nothing has changed, it seems to me.
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26220
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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++;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26220
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: October 04, 2012, 05:09:34 am by AWOL » Logged

"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.

Pages: [1] 2 3 ... 6   Go Up
Jump to: