Pages: 1 [2]   Go Down
Author Topic: decimal point in temp reading  (Read 2440 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 48
Posts: 1630
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You still need to do this so that the initial value of averageTemp isn't random.
Code:
float averageTemp = 0; // create a float to hold running average

But I don't understand what that for loop does. If you want the average of a thousand consecutive readings then that loop doesn't do it.

Pete
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


@02660 - Are you just wanting to convert a float value to a nul-terminated array of characters with one digit to the right of the decimal point?

(Trying to understand the problem)
Logged

There's always a better way!

The people's republic of Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You still need to do this so that the initial value of averageTemp isn't random.
Code:
float averageTemp = 0; // create a float to hold running average

But I don't understand what that for loop does. If you want the average of a thousand consecutive readings then that loop doesn't do it.

My understanding was if it was not defined it would be 0.  

I got the code from another sketch, my understanding was it was to be averaging the result over a thousand readings.
What is wrong with it? / the proper way would be?
Logged

The people's republic of Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
convert a float value to a nul-terminated array of characters with one digit to the right of the decimal point?

At this time I have no idea what a "nul-terminated array of characters" is exactly...  smiley-eek

My original request was just to get my value from the Thermister() function to show one digit after the decimal point.  This was accomplished with a proper print statement.

And in doing that the incorrect code was mentioned regarding the averaging of the sensor reading.  So the thread has kinda split....

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49317
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Or the forum settings could be changed to allow larger posts...
You get what you pay for.
Logged

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

Or the forum settings could be changed to allow larger posts...  smiley-wink

I don't want to see lengthy posts of mindless gibberish, personally. smiley-wink

That's why the limits are there.
Logged


Offline Offline
Edison Member
*
Karma: 48
Posts: 1630
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is wrong with it?

We can try to find out what it does by seeing what it actually calculates after the first few times round the loop.
float AvrageTemp(int analog_pin){
 float averageTemp = 0; // create a float to hold running average
 for (int i = 1; i < 1000; i++) // start at 1 so we dont divide by 0
    averageTemp += ((Thermister(analog_pin) - averageTemp)/(float)i); // get next sample, calculate running average
 return averageTemp; // return average temperature reading
}

To make this briefer for me to type I'll refer to the avergeTemp calculated when at step i as just avgi (e.g. avg1, avg2 etc) and the sequence of Thermistor readings as T1, T2, etc.
In my version, avg0 is initialized to zero.
Therefore the result of avg1 = (T1 - avg0)/1 = T1. This one is correct. The average of one reading is just that reading.
Step 2: avg2 = (T2 - avg1)/2 = (T2 - T1)/2. This is clearly wrong. The average of the two values should be (T2 + T1)/2
Step 3: avg3 = (T3 - avg2)/3 = (T3 - (T2 - T1)/2)/3 = ( 2*T3 - T2 + T1)/6. Nope.
Step 4: avg4 = (T4 - avg3)/4 = (T4 - (2*T3 - T2 + T1)/6) / 4 = (6*T4 - 2*T2 + T2 - T1)/24. It's getting worse.
If you assume that all the readings are identical and equal a value T, then at any step of the loop it should produce the value T as the average but after step four, if each Ti = T, then the result of the fourth step is T/6. Not even close to the average.
So, what is wrong with it is that it doesn't compute an average - "running" or otherwise.
There's also one other minor booboo in the code. The for loop only goes round 999 times - not 1000

Quote
/ the proper way would be?
If it is supposed to produce the average of one thousand consecutive readings then it could be done like this:

Code:
float AvrageTemp(int analog_pin){
  // Holds the average
  float averageTemp = 0;
  // Calculate the average of one thousand consecutive readings of the thermistor
  for (int i = 0; i < 1000; i++) {
    averageTemp += Thermister(analog_pin)/1000.;
  }
  // return average temperature reading
  return averageTemp;
}

Pete
Logged

Pages: 1 [2]   Go Up
Jump to: