Temperature Averaging

Hello, I’m in the midst of making a project which utilizes 3 temperature sensors. The problem I’m encountering is that when I try to average the readings, it seems the max I can sample is 3 before all I get is zeros. This is with the while loop, originally I tried a FOR loop and the max sample rate was 2 for that? Seems weird to me but let me explain how this happens. Initially when the code is uploaded with a higher sample rate, let’s say 10, the code will work perfectly but once I unplug the USB and plug it back in via USB or wall jack all I will get is the zeros. It will only work with a maximum of 3 (or 2 with FOR) when I unplug it. Resetting has no effect either way, when its working it stays working, when its unplugged it still won’t work. Here is the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd (13,12,7,6,5,4);

void loop () {
  lcd.clear(); // clear LCD screen
  tempS(1);
  tempS(2);
  tempS(3);
  delay(350); 
};

void setup() 
{
  lcd.begin(20, 4);    // need to specify how many columns and rows are in the LCD unit
  lcd.setCursor(0,0);
  lcd.print("Reef Controller");
  lcd.setCursor(0,1);
  lcd.print("test * v0.4");
  lcd.setCursor(0,2);
  lcd.print("TEMP");
  delay(2000);
  
}

void tempS(int sensor)
{
  int nsamp = 3; 
  float temp;
  float tempValue;
  int j = 0;
    //for (byte j=0;j<nsamp;j++)
    while (j < nsamp)
{
  tempValue =tempValue + analogRead(sensor);
  j++;
}
  tempValue=tempValue/nsamp;
  temp = tempValue;
  temp = temp*5/1024.0;

  //temp = analogRead(sensor)*5/1024.0;
  temp = temp - 0.5;
  temp = temp / 0.01; 

  if (sensor == 1)
  {
  lcd.setCursor(0,0);
  }
  if (sensor == 2)
  {
  lcd.setCursor(0,1);
  }
  if (sensor == 3)
  {
  lcd.setCursor(0,2);
  }
  lcd.print("Temp");
  lcd.print(sensor,DEC);
  lcd.print(": ");   
  lcd.print(temp,0);       //# means decimal places
  lcd.print((char)223);    //degree
  lcd.print("C");

  
}

Any help would be appreciated, thanks!

You haven't initialized tempValue in your tempS routine. It's a local so it will not be done for you. Set it to 0.0.

It may be worth changing the constants you're using with float values to be float too, e.g. 5.0, not 5.

Put some Serial.prints in to see what your intermediate values are.

Ok I’ll try that when I get home, I did try at one point changing all values to floats but I did not change the 5 to have a decimal place. Do you have any clue why it would work while it’s plugged in when first programmed but not after it’s powered down?

another way to average is to use a running average; give it a try

float tempValue = 0.0;

void tempS(int sensor)
{
  float tempValue = analogRead(sensor) * 0.48828125 - 50;   // shrunk the formula to the max  ((AR * 5.0 /1024.0) -0.5) / 0.01

  temp = 0.85 * temp + 0.15* tempValue; // new value counts for 15%  <<<< this is the running average thingie, you can change the %% as long as the both add up to 1.0

  lcd.setCursor(0, sensor -1);  // optimized triple if 

  lcd.print("Temp");
  lcd.print(sensor, DEC);
  lcd.print(": ");   
  lcd.print(temp, 2);       // # means decimal places
  lcd.print((char)223);    // degree
  lcd.print("C");
}

Just like to say thanks for the help guys, turned out it was because I did not initialized the variables to zero before I used them. Now it works great, Thanks!