decimal point in temp reading

It does RETURN a value...

Sure, and that value is a double. Therefore, it has, by definition, a fractional part.

That you do not see the fractional part means that whatever code is showing you the value is doing so in a way other than what you want. But, we can't see that code.

Fare enough,

vfd.print(int(Thermister(analogRead(1))));

So I am thinking it's the int()?
How do I only display one past the decimal point?

 float averageTemp; // create a float to hold running average
 for (int i = 1; i < 1000; i++) // start at 1 so we dont divide by 0
    averageTemp += ((Temp - averageTemp)/(float)i); // get next sample, calculate running average

This code doesn't do anything useful.
The declaration of averageTemp will cause it to be initialized with a random value which makes the for loop start its "running average" with a random value.
The for loop uses the same value of Temp every time so it is not generating a running average.

Pete

02660:

vfd.print(int(Thermister(analogRead(1))));

Debugging small sections of code never makes it simple for those trying to help you. What vfd library are you using? Does it support printing floats?

So I am thinking it's the int()?

Yes. That is casting the double to an int.

How do I only display one past the decimal point?

The first thing that you need to do is get rid of the int() part. The, you may or may not actually be able to do what you want. Again, we can't help because we have no idea what class the vfd object is an instance of.

The VFD is using, #include <Ncr4X20Vfd.h>
It will print xx.xx with the int() removed from the print statement.

I am also displaying the value on webpage using the Ethernet shield with client.print() each behave the same so far.

As for posting the full code the forum will not allow me to, it exceeds the max characters allowed....

el_supremo:

 float averageTemp; // create a float to hold running average

for (int i = 1; i < 1000; i++) // start at 1 so we dont divide by 0
    averageTemp += ((Temp - averageTemp)/(float)i); // get next sample, calculate running average




This code doesn't do anything useful.
The declaration of averageTemp will cause it to be initialized with a random value which makes the for loop start its "running average" with a random value.
The for loop uses the same value of Temp every time so it is not generating a running average.

Pete

Good point, I am open to suggestions.

If the Ncr4X20Vfd class derives from Print, then adding a 2nd argument to the vfd.print() call will allow you to specify the number of decimal places.

vfd.print(Thermister(analogRead(1)), 1);

should show one decimal place.

The Client class does derive, at least indirectly, from the Print class, so the optional 2nd argument CAN be used to limit (or increase) the number of decimal places.

Thank you, I did not know you could do that.

02660:
As for posting the full code the forum will not allow me to, it exceeds the max characters allowed....

Attaching the project file or pastebin.com are alternative options.

el_supremo:

 float averageTemp; // create a float to hold running average

for (int i = 1; i < 1000; i++) // start at 1 so we dont divide by 0
    averageTemp += ((Temp - averageTemp)/(float)i); // get next sample, calculate running average




This code doesn't do anything useful.
The declaration of averageTemp will cause it to be initialized with a random value which makes the for loop start its "running average" with a random value.
The for loop uses the same value of Temp every time so it is not generating a running average.

Pete

How about this,
I just split it back into two classes so "Temp" would change on every evaluation.

/*

  • Thermistor Function, Read thermistor and return a value in deg. F
    /
    double Thermister(int RawADC) {
    double Temp;
    Temp = log(((10240000/RawADC) - 10000));
    Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))
    Temp );
    Temp = Temp - 273.15; // Convert Kelvin to Celcius
    Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
    return Temp;
    }

/*

  • avrage temp sensor reading
    */
    float AvrageTemp(int analog_pin){
    float averageTemp; // 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
    }

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

You still need to do this so that the initial value of averageTemp isn't random.

 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

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

el_supremo:
You still need to do this so that the initial value of averageTemp isn't random.

 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?

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

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

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

You get what you pay for.

02660:
Or the forum settings could be changed to allow larger posts... :wink:

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

That's why the limits are there.

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 = ( 2T3 - T2 + T1)/6. Nope.
Step 4: avg4 = (T4 - avg3)/4 = (T4 - (2
T3 - T2 + T1)/6) / 4 = (6T4 - 2T2 + 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

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

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