Hello all,
This is my very first attempt at programming anything so sorry for the beginner questions....
First of all,
I want to use an arduino as a temperature display using a two wire sensor.
I have connected it up using a voltage diving circuit with a R1 value of 2kohms. Supply voltage is a measured 5.09v
I can have the script return the correct resistance R2, however i come unstuck when using a lookup table to convert the resistances into known temperatures.
Please help
Code is
//ths is the pin on the arduino that the input is connected to//
int analogPin= 0;
//this is the interger read at the input pin//
int raw= 0;
//this is the set voltage input into the voltage divider to calculate the unknown resistance//
float Vin= 5.09;
//ths is the voltage accross the unknown resistor//
float Vout= 0;
//this is the fixed known resistance//
float R1= 2000;
//this is the unknown resistor//
float R2= 0;
float buffer= 0;
//this is the lookup table for the thermistor properties (resistance at known temp values)//
long lookupR[] = {
45313, 26114, 15462, 9397, 5896, 3792, 2500, 1707, 1175, 834, 596, 436, 323, 243, 187, 144, 113, 89};
long lookupT[] = {
-40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130};
void setup()
{
Serial.begin(9600);
}
void loop()
{
//this is reading the interger value at input pin and storing it as (raw)//Â
raw= analogRead(analogPin);
if(raw)
{
//this is the calculation of the unknown resistance//Â
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= ((R1*Vout)/(Vin-Vout));
R2= buffer;
//This is ment to lookup the table for the temperature based on the calculated resistance calculated ealier(doesnt work)//
long lookup(long R) {
int x = 0;
for (int i=0; i <= 17; i++){
if (R < lookupR[i]) {
 x = i;
}
}
return x;
}
Serial.println(raw);
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
delay(1000);
}
}
//this is reading the interger value at input pin and storing it as (raw)//
raw= analogRead(analogPin);
if(raw)
{
Does that comment add any value? Anyone can see that the code is doing exactly what the comment says. Comments should explain WHY that code is there, not what it is obviously doing.
Why does raw need to be a global variable? What other functions are using it? It there ARE any, why aren't you passing the value to them?
Why are you treating raw as though it was a boolean variable in the if statement?
buffer= raw * Vin;
You obviously don't know what a buffer is.
Why is that variable global? Why should we have to scroll up past all those useless comments to find its type?
You can NOT define a function inside another function. Get lookup() out of loop().
First, do you know how you'd perform the task "manually"?
You'll need to traverse through your lookupR[] array comparing its values to the new resistance that you measured / computed. You'll likely find that your measured value falls between two array entries.
Then, take the indices of those two lookupR[] values and use them to get the two corresponding temperature values from the lookupT[] array.
Then, do a linear interpolation between those temperature values.
But, you'll also have to handle 3 special cases:
Your measured resistance is EXACLY equal to one of the values in your lookupR[] array.
Your measured resistance is greater than the largest value in your lookupR[] array.
Your measured resistance is less than the smallest value in your lookupR[] array.
As written, the code does not compile. Some ideas:
You need to move the function definition for your lookup() function outside of any other function (i.e., take lines 43-51 and move them outside of loop()).
In your lookup() function, you don't need variable x. If you met the conditions for a successful pass through the lookup table, why continue to look through the rest of the values? Instead of the statement x = i; why not just use return i; instead? (I might not understand what you're doing here.)
Given the range of your data, the lookupR[] array could be defined as an unsigned int and and lookupT[] could be a byte. Saving memory is a good thing.
Anytime you initialize a float data type, use a decimal point: float R2 = 0.0; While the compiler can figure this out ok, it helps document the data definition.
Use symbolic constants and macros to make the code easier to read/change. For example:
#define ANALOGPIN 0*
// ...more code...*
raw = analogRead(ANALOGPIN);*
Using upper case makes it easier to see that it's a symbolic constant. Now, when you realize that you're using the wrong pin for an analog value, just change the #define number, recompile, and everything is automatically changed.
Avoid "magic numbers" in your code, like the 17 in: for (int i = 0; i <= 17; i++) { If you change the number of elements in the array, now you have to search the code for that element count. Instead, use this macro:
#define ELEMENTCOUNT(x) (sizeof(x) / sizeof(x[0])) // Put near the top of the program*
// ...more code...*
for (int i = 0; i < ELEMENTCOUNT(lookupR); i++) {*
Now if you add more data to the array, the compiler automatically resizes your loop. Note that this macro is "typeless". That is, you can use it with any array data type. Also note the change to the '<' operator in the second expression.
With your cursor in the Source Code Window of the IDE, use Ctrl-T to reformat your code into a more common C style.