SW help needed Arduino


#1

Hi

I'm trying to make an average on a NTC sensor but seems that averaging it out gives wrong value in return stringResult;.

Sensor I'm using is:

http://www.seeedstudio.com/depot/Temperature-sensor-with-steel-head-p-78.html

Trying to find the fault in String Temp_water() I have added 3 Serial.print


First one gives correct value out - This one:

Serial.println (readings[index]);

Next 2 are off in values, but I can't see why...

Serial.println (total); and

Serial.println (average);

Any ideas on what I'm doing wrong as I'm noob with this...

#include <math.h>

float water_temp; //

float Thermistor(int RawADC)
{

long Resistance;
float Temp; // Dual-Purpose variable to save space.
float pad = 10010; // balance/pad resistor , set this to
// the measured resistance of your pad resistor


Resistance=((1024 * pad / RawADC) - pad);
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius

water_temp = Temp;

return Temp;

}

String Temp_water()
{
digitalWrite (Vcc_temp_water ,HIGH); // Enable VCC

delay(50); // Delay a bit... to stabilize VCC

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 5;

static float readings[numReadings] = { 0.0 }; // the readings from the analog input

static int index = 0; // the index of the current reading

static float total = 0; // the running total

static float average = 0; // the average

static bool initialized = false;


if (!initialized) {
average = Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
for (int no = 0; no <= numReadings; no++)
{
readings[no] = average;
total = total + average;
}
initialized = true;
}

// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
Serial.println (readings[index]);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
Serial.println (total);
average = total / numReadings;

Serial.println (average);

String stringResult = "";
int temp_heltal = (int)average;
int temp_decimal = (int)((average - (float)temp_heltal)*100);
stringResult = stringResult + temp_heltal + "," + temp_decimal;

digitalWrite(Vcc_temp_water ,LOW); // disable VCC

return stringResult;


}


#2

Forgot 2 runs on the sw.

Serial.println (readings[index]);24.18
Serial.println (total);145.07
Serial.println (average);29.01

Bit heat from my hand on sensor and temp goes up:
Serial.println (readings[index]);27.31
Serial.println (total);152.85
Serial.println (average);30.57

Value of average matches ok with total as that is 1/5 value


#3

Looks like I found the fault as when you start out with const int numReadings = 5; It's actually 6 times as it begins from 0 to 5 = 6.
Fix is : for (int no = 0; no < numReadings; no++)