EcoRenovator

EcoRenovator (https://ecorenovator.org/forum/index.php)
-   Geothermal & Heat Pumps (https://ecorenovator.org/forum/forumdisplay.php?f=10)
-   -   Arduino issues (https://ecorenovator.org/forum/showthread.php?t=2909)

Mikesolar 03-02-13 05:34 PM

Arduino issues
 
OK, even though Arduino is supposed to make us older farts equal to the young dudes in programming, I will readily admit that I am useless at it which is even more embarrassing as I was an electronics technologist 30 years ago. ARRRGGGG.

So all I am trying to do is take the Mega, and sensor shield, add a 10k thermister (2 wire, not the digital stuff that I never see in the heating industry) and see the temp change on the screen.

I followed a thread on yourduino to do this and got errors that I cannot explain and it seems, I cannot even find definitions to some of the phrases given at the bottom of the page when trying to verify the program.

Here is an example:

"was not declared in this scope"

What does that mean? I cannot find a glossary of terms used anywhere. HHHheeeellllpppp (please)

RobbMeeX 03-03-13 01:35 AM

I am no pro, but is this regarding a variable that was not declared initially? Do you want to post up your sketch?

Mikesolar 03-03-13 06:10 AM

The sketch is not important so here only because it it taken directly from the the adafruit book about how to use a thermister. I cut and paste the exact sketch from the site and voila......errors.

The phrasing doesn't have enough explanation. Here is what i tried to do:

Code:


// the value of the 'other' resistor
#define SERIESRESISTOR 10000   
 
// What pin to connect the sensor to
#define THERMISTORPIN A0
 
void setup(void) {
  Serial.begin(9600);
}
 
void loop(void) {
  float reading;
 
  reading = analogRead(THERMISTORPIN);
 
  Serial.print("Analog reading ");
  Serial.println(reading);
 
  // convert the value to resistance
  reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Thermistor resistance ");
  Serial.println(reading);
 
  delay(1000);
}

This works well enough. What I get is the thermister value which changes as it should.

Then I erase this program and copy one that changes the thermister value to deg C (below) and it doesn't recognize the thermister.

Code:


// which analog pin to connect
#define THERMISTORPIN A0       
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000     
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25 
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000   
 
int samples[NUMSAMPLES];
 
void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL);
}
 
void loop(void) {
  uint8_t i;
  float average;
 
  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
  samples[i] = analogRead(THERMISTORPIN);
  delay(10);
  }
 
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
    average += samples[i];
  }
  average /= NUMSAMPLES;
 
  Serial.print("Average analog reading ");
  Serial.println(average);
 
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  Serial.print("Thermistor resistance ");
  Serial.println(average);
 
  float steinhart;
  steinhart = average / THERMISTORNOMINAL;    // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                  // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                // Invert
  steinhart -= 273.15;                        // convert to C
 
  Serial.print("Temperature ");
  Serial.print(steinhart);
  Serial.println(" *C");
 
  delay(1000);
}

It says it is -273degC and thermister resistance is "inf"

The problem is that the explanations are inadequate.

AC_Hacker 03-03-13 09:00 AM

Quote:

Originally Posted by Mikesolar (Post 28621)
The sketch is not important...

If I understand your post, you are having problems with your sketch. So, it seems that since you are having problems with your sketch, it actually is important... especially if you want someone else to help you. The de-bugger in Arduino is not the best in the world, but it is better than nothing. Sometimes there is an error in some part of the code, and the Arduino de-bugger says it's in another place, so if you do not include your entire sketch, you are wasting everyones time.

It is easiest to see your sketch, if you put it inside CODE tags.

So you would use a square brace "[" and then you would put the letters "CODE" next, and then you would put another square brace "]"... then you put your sketch, your whole sketch next...

...then you would use a square brace "[" and then you would put a slash "/" and then the letters "CODE" next, and then you would put another square brace "]".

It makes your sketch stand out so that it's easier to see what went wrong.

-AC

Mikesolar 03-03-13 09:24 AM

The two versions of the code are presented in their entirety above. I thought it was evident enough, perhaps not, so I will try again. I edited it again to show the square brackets. As there is 2 separate codes in the post I said "end post". This is not going in the arduino so i assume it is just for the readers benefit.

The main thing is that there is no plain english glossary that really defines the terms as presented in the debugger. I have my talents but this isn't one of them LOL.

jeff5may 03-03-13 12:40 PM

i'm no longer a programming expert, but this line looks fishy to me:
steinhart -= 273.15; // convert to C

it seems you should have something like:
variable = variable + constant
rather than
variable = constant

Mikesolar 03-03-13 01:25 PM

The original sketch looks at the value but does not convert to C nor does it account for the non linearity of the thermister. The Steinhart equation is supposed to do this, to some extent.

Jeff, would you get rid of the steinhart parts? It does need some math to convert.

jeff5may 03-03-13 05:34 PM

If it were me, I would use what works and build upon it.
Since the original routine reads the thermistor value as it should, I would use it.
It returns the floating point variable READING.
I would take the first code and add a single line that converts the reading to temperature.
Something like this:

float logcubed = log(reading);
logcubed = logcubed * logcubed * logcubed;
float kelvin = 1.0 / (-7.5e-4 + 6.23e-4 * log(reading) - 1.73e-6 * (logcubed));
float celsius = kelvin - 273.15;

the A, B, and C values were stolen from Alan Wendtt from his arduino.cc posting. Your values may or may not vary, IDK.

I learned long ago that the compilers/assemblers/translaters (whatever you call themtoday) tend to work better with these ugly one-line equations without hiccups or weird errors than using many lines to do the same thing.

Once you have the code in that first program spitting out temperatures, use it as a function to do your averaging. As long as you keep the operations separate it will be easy to tell where your program is going wrong.

jeff5may 03-03-13 06:20 PM

I found an article that is aimed at doing the averaging/smoothing, written in older-school syntax.

Arduino - Smoothing

From this example, it seems to me that you haven't defined your SAMPLES array in the second program correctly. I could be wrong, but constants and variables used to have to be assigned a data type before you could assign them actual values.
If you are looking for a rolling average, a defined array isn't even necessary.
You could simply make a low-pass filter, PID style.
Here's a snip in arduino flavor.

float adcResult = 0;

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

void loop()
{
adcResult = adcResult * 0.95 + analogRead(0) * 0.05;
Serial.println(adcResult);
delay(50);
}

in this example, alpha is 20, which approximates a 20-point running average.

Piwoslaw 03-04-13 04:03 AM

Quote:

Originally Posted by jeff5may (Post 28625)
i'm no longer a programming expert, but this line looks fishy to me:
steinhart -= 273.15; // convert to C

it seems you should have something like:
variable = variable + constant
rather than
variable = constant

The line:
Code:

steinhart -= 273.15;
is the same as:
Code:

steinhart = steinhart - 273.15;
so this in itself is not a problem.

As for the
Quote:

was not declared in this scope
, this means that a variable was not defined inside the function using it (inside main(), for example), nor was it defined globally (before main()). This is an error in C/C++ programming, not Arduino-specific.
If possible please give the full error message, so that we know which variable was not declared, etc.

Mikesolar 03-04-13 05:50 AM

Here is the current code which is purely a copied code from the Adafruit site "using a thermister". Then, I added the last line from Jeffs approach in bold. It seems to ignore the added code and continues to post in ohms. I am looking for a reason for this but the program doesn't come up with any errors.

Code:


// the value of the 'other' resistor
#define SERIESRESISTOR 10000   
 
// What pin to connect the sensor to
#define THERMISTORPIN A0
 
void setup(void) {
  Serial.begin(9600);
}
 
void loop(void) {
  float reading;
 
  reading = analogRead(THERMISTORPIN);
 
  Serial.print("Analog reading ");
  Serial.println(reading);
 
  // convert the value to resistance
  reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Thermistor resistance ");
  Serial.println(reading);
 
  float logcubed = log(reading);
logcubed = logcubed * logcubed * logcubed;
float kelvin = 1.0 / (-7.5e-4 + 6.23e-4 * log(reading) - 1.73e-6 * (logcubed));
float celsius = kelvin - 273.15;

 
  delay(1000);
}


AC_Hacker 03-04-13 10:05 AM

Quote:

Originally Posted by Piwoslaw (Post 28634)
The line:
Code:

steinhart -= 273.15;
is the same as:
Code:

steinhart = steinhart - 273.15;
so this in itself is not a problem.



I think this is an illegal line... with the minus in front of the equal sign:

Code:

steinhart -= 273.15;
I you want steinhart to take the value of 273.15, you could say:

Code:

steinhart = 273.15;
I you want steinhart to take the value of -273.15, you could say:

Code:

steinhart = -273.15;
... or even:

Code:

steinhart = 273.15 * -1;
If you want to increase whatever value steinhart already has by 273.25, you could say:

Code:

steinhart = steinhart + 273.15;
If you want to decrease whatever value steinhart already has by 273.25, you could say:

Code:

steinhart = steinhart - 273.15;

& & &


The part of the program that is at the top, in other words above...

Code:

void setup()  {
... is where all variable name must be declared, or else the program will not know what the strange undefined words mean.

steinhart is a variable name and it needs to be defined as such in this section.

The middle part of the program, that is below...

Code:

void setup()  {
but above...

Code:

void loop()
Is where initial values are set... in other words, you can't assume that the first time you use steinhart that it will have a value of zero, you have to make it so.


& & & & & &


You can't expect anyone to give useful advice on a program or on what it's debugger message means unless you have quoted ALL of the program and ALL of the debugger message.

Else, you're wasting everyones time, including your own.

-AC

Piwoslaw 03-04-13 02:21 PM

Quote:

Originally Posted by AC_Hacker (Post 28640)
I think this is an illegal line... with the minus in front of the equal sign:

Code:

steinhart -= 273.15;
[...]

Compound assignment operators in C and C++

+= plus equal sign

Quote:

Originally Posted by AC_Hacker (Post 28640)
The part of the program that is at the top, in other words above...

Code:

void setup()  {
... is where all variable name must be declared, or else the program will not know what the strange undefined words mean.
[...]

I may be wrong (I haven't installed the Arduino compiler on my new computer yet, so I can't try it today), but a variable defined in setup() may not be visible in loop() since those are two distinct functions. If you want a variable to be visible in both setup() and loop(), it must be declared outside of the scope of either function, ie before the declaration of setup() (after the #define would be good).

Mikesolar 03-04-13 08:44 PM

I'm trying to recreate the sketch which i had deleted by accident. I will then post it. If I find a way around it I will post that too.

mrd 03-04-13 09:30 PM

Quote:

Originally Posted by Mikesolar (Post 28638)
It seems to ignore the added code and continues to post in ohms.

What it 'posts' is the value passed to the function Serial.println. That function name indicates you are utilizing the serial port to print a line.

The value you are passing in your new code is the same passed in the original:
Code:

  Serial.print("Thermistor resistance ");
  Serial.println(reading);

If you want to print something else you need to specify such, e.g.
Code:

float logcubed = log(reading);
logcubed = logcubed * logcubed * logcubed;
float kelvin = 1.0 / (-7.5e-4 + 6.23e-4 * log(reading) - 1.73e-6 * (logcubed));
float celsius = kelvin - 273.15;

Serial.println(celsius);

And note the correct value is placed into the variable celsius before you pass it to the Serial.println function to be displayed.

Also, you should paste the full error message you are receiving with the other code you are trying to use. Compiler errors typically indicate a line number at the beginning to help pinpoint the source of the mistake. Variable scope relates to which portions of your code may access a given variable. This scope is determined by where the variable is defined. For example, function-local scope is variables defined within a function (within the brackets { } encapsulating the function definition) whereas module/file-level scope is variables defined outside of any function, which can be accessed within any function.

I could go on and on but it sounds like you're just trying to get your feet wet and I don't want to drown you. Also, take everything I say with a grain of salt. I've never coded for the Arduino, but I am familiar with C/C++, and like to make assumptions.

jeff5may 03-04-13 10:43 PM

Thanks to all who are helping set things in the right direction. I have an arduino and have been playing with it, but my programming skills are rusty. I have very little experience programming in languages newer than say, pascal or plain old c. Sorry about my misunderstanding of abbreviated operations statements.

Mikesolar,

I know you've seen lots of those shows where a writer starts a story and changes his mind. He crumples up his paper and starts over. After the commercials, we return to the author still writing, with a basket full of crumpled up papers at his feet. He then writes his masterpiece.

This is a prime example of computer programming. Never mind the program that didn't work for the moment, you're on the right track. What code you have now is reading your thermistor and calculating ohms, kelvin and celsius. Just because those variables aren't being printed doesn't mean they don't exist. I'm guessing you will be able to serial print out any of these values at will if desired. So you have succeeded in the data acquisition phase of your program.

The next thing you want to do is figure out which method to use in averaging this raw data. A rolling average will fit into this existing code nicely, but a circular array will take some rearranging.


All times are GMT -5. The time now is 04:38 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Ad Management by RedTyger