The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« on: January 30, 2012, 06:45:19 pm » |
I have pieced this code together to read and average thermistor readings. It works fine but I am trying to figure out how to show it to one decimal. output as is = xx F I would like = xx.x F /* * Thermistor Function, read thermistor and return an averaged value */ 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; 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 return averageTemp; // return average temperature reading }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #1 on: January 30, 2012, 07:04:41 pm » |
That code does not output the temperature. Therefore, no code changes are required IN THAT CODE.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #2 on: January 30, 2012, 07:07:35 pm » |
That code does not output the temperature. Therefore, no code changes are required IN THAT CODE.
I suppose it depends on what your definition of output is. It does RETURN a value...
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #3 on: January 30, 2012, 07:10:27 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #4 on: January 30, 2012, 07:18:14 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 13
Posts: 899
|
 |
« Reply #5 on: January 30, 2012, 07:21:32 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5172
CMiYC
|
 |
« Reply #6 on: January 30, 2012, 07:24:31 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #7 on: January 30, 2012, 07:25:59 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #8 on: January 30, 2012, 07:37:38 pm » |
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....
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #9 on: January 30, 2012, 07:38:43 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #10 on: January 30, 2012, 07:45:28 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #11 on: January 30, 2012, 07:55:12 pm » |
Thank you, I did not know you could do that.
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5172
CMiYC
|
 |
« Reply #12 on: January 30, 2012, 07:57:02 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #13 on: January 30, 2012, 07:59:06 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #14 on: January 30, 2012, 08:01:11 pm » |
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. Or the forum settings could be changed to allow larger posts... 
|
|
|
|
|
Logged
|
|
|
|
|
|