[SOLVED] Temperature trend calculation problem

Hi all,

I am mesuring temperature via two sensors. To smooth things out, I am making a weighted average of the historical readings vs the new reading (the tempC function is to read the thermistor via the analog pins):

  avgFridgeTemp = 0.3 * tempC(fridgeTempPin) + 0.7 * avgFridgeTemp;
  prevAvgFridgeTemp = avgFridgeTemp;

and

  avgFreezerTemp = 0.3 * tempC(freezerTempPin) + 0.7 * avgFreezerTemp;
  prevAvgFreezerTemp = avgFreezerTemp;

I have then written a function that is supposed to print an arrow on the LCD, depending on the trend:

void calcTrend(float prevAvg, float currAvg) {
  if (currAvg > 1.05 * prevAvg) {
    lcd.setCursor(19,1);
    lcd.write(2);  // trend is up
  }
  if (currAvg >= 1.05 * prevAvg && currAvg <= 0.95 * prevAvg) {
    lcd.setCursor(19,1);
    lcd.write(3); // trend is steady
  }
  if (currAvg < 0.95 * prevAvg) {
      lcd.setCursor(19,1);
      lcd.write(4); // trend is down
   }
}

However, there is something wrong with the function.
I have put this ±5% margin because the readings are not steady, they change by a few 1/10th of a degree with every reading (even with the smoothing).
I'm getting very confused when it comes to intervals and I can't see what's wrong... Any suggestions?

Your >= and <= are backwards in the middle If.

It still doesn't work, and doesn't display any arrow at all.
I tried the arrows and they work, so it seems that the Arduino can't find any if() statement fitting the situation.

This:

avgFridgeTemp = 0.3 * tempC(fridgeTempPin) + 0.7 * avgFridgeTemp;
  prevAvgFridgeTemp = avgFridgeTemp;

ensures that prevAvgFridgeTemp and avgFridgeTemp will always be the same. Set the prev reading before you compute the new average.

Then, as already noted, your middle if is broken, which is the one you want in the situation where the readings are (have been forced to be) the same.

Thanks, I put this:

prevAvgFridgeTemp = avgFridgeTemp;

at the end of the loop

and reversed the signs in the middle if():

void calcTrend(float prevAvg, float currAvg) {
  if (currAvg > 1.05 * prevAvg) {
    lcd.setCursor(19,1);
    lcd.write(2);  // trend is up
  }
  if (currAvg <= 1.05 * prevAvg && currAvg >= 0.95 * prevAvg) {
    lcd.setCursor(19,1);
    lcd.write(3); // trend is steady
  }
  if (currAvg < 0.95 * prevAvg) {
      lcd.setCursor(19,1);
      lcd.write(4); // trend is down
   }
}

The up and down trends are now working fine, but the steady is not working.

Are parentheses needed?

((currAvg <= 1.05 * prevAvg) && (currAvg >= 0.95 * prevAvg))

I would put them in for readability sake.

Yep, that fixed it, thanks!

It feels great to be helpful!
It's easier for everyone when the person who needs help listens like you did.
I wish everyone was so capable of listening to suggestions...