Having trouble on solving the following tasks

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

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

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

When is our homework assignment due in?

Jonraptor: 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.

dxw00d: When is our homework assignment due in?

I dunno, haven't checked tbh

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:

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)

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:

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

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

Also, try this sketch first:

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

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

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");
}
float ThermResist = 0.0;
...
...
      if(ThermResist == Table[4][1])

Nothing has changed, it seems to me.

AWOL:

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.

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

AWOL: 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.

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

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

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.

AWOL: if(ThermResist == Table[4][1]) ?

If the ThermResistance is = to the output shown on the table row 4, the 2nd column. Then, it will trigger the printout of the temperature on the same row on column 0

Jonraptor:

AWOL: if(ThermResist == Table[4][1]) ?

If the ThermResistance is = to the output shown on the table row 4, the 2nd column. Then, it will trigger the printout of the temperature on the same row on column 0

I guess you ignored my previous comment then ?

tuxduino:

Jonraptor:

AWOL: if(ThermResist == Table[4][1]) ?

If the ThermResistance is = to the output shown on the table row 4, the 2nd column. Then, it will trigger the printout of the temperature on the same row on column 0

I guess you ignored my previous comment then ?

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

You’re stepping through a lookup table, but only ever using constant subscripts.
That doesn’t make sense.

Each loop has double increments on it.
That doesn’t make sense.

You’re looking for equality with floating point values.
That doesn’t make sense.

AWOL: You're stepping through a lookup table, but only ever using constant subscripts. That doesn't make sense.

Each loop has double increments on it. That doesn't make sense.

The For loop statement is suppose to provide a loop. One for the rows(0-9), and the other for the cols(0-1). Inside both loops, i am trying to state an if statement which applies to the Thermresist if it hits on a certain point based on table[4][1] 3800, then it will trigger a printout statement. Otherwise, it continues on with its sensorval, volt, and ohm printout process. This is what i am trying to do. But it doesn't seem to work out well for me. If this doesn't make any sense to you, can you please provide an explanation on what your suggestion going to be on this occassion?