Change Color After Time Length

I'm have an arduino which is able to read temperature via an RTD. It takes a water temp reading every 10th of a second (or so). I want to have the led change color based on a temperature BUT only if the temperature is stable. For example, the temp. may read something like this over 10 seconds:

50.1

50.1

50.4

49.7

49.9

50.0

50.7 etc...

Problem is the temperature is never stable enough to always read higher than the last reading. This in turn makes for a real tough time coding my LED's to make a smooth transition. So...what I'm proposing (and need help with ) is a way to tell it to change color if the temp is.... lets say ...above 50C for the last 10 seconds. Having the 10 seconds delay before transitioning to the color will ensure me that is has definitely switched to a temp. state that is above 50C. I don't know how to code this! Any help would be great! I will put the login in code form below but need help with the actual code:

     if (tempC >= 50 for last 10 seconds (or last 100 reads because 10per sec * 10 sec = 100){

              change color from blue to red;
                            }

If I understand correctly:

  • One hundred “hot” (50°C or higher) temperature readings in a row are needed to turn the red light on.
  • A single “not hot” (lower than 50°C) temperature reading is sufficient to prevent the red light from turning on.

The logic I would use is:

  • Keep a count of how many “hot” temperature readings you get.
  • If you get a “not hot” reading, then start counting over again from zero.
  • If your count reaches 100, then you know that the last 100 readings have all been “hot”.

There are a couple of techniques for something like this.

First, a low-pass filter:

filteredData = 0.75 * filteredData + .25 * newData

This smooths out the data. Use float for the filtered data becausse it’s a statistical rolling average.

Next, you need hysteresis. The basic idea is that rather than a fine line between a and b, there’s a fudge-factor. To switch from a to b, the reading has to get past that fudge factor.

int tenDeg; // '1' means 10-20, '2' means 20-30, etc.
const int fudgeFactor = 1.5;

/**
 * -1 : temp has dropped
 *  0 : temp has not changed
 * +1 : temp has increased
 */

int checkTemp(float filteredReading) {
  if(filteredReading > (tenDeg+1) * 10 + fudgeFactor) {
    tenDeg = filteredReading / 10;
    return 1;
  }
  else if(filteredReading < tenDeg * 10 - fudgeFactor) {
    tenDeg = filteredReading / 10;
    return -1;
  }
  else {
    return 0;
  }
}

PaulMurrayCbr: int tenDeg; // '1' means 5-15, '2' means 15-25, etc.

The cutoff point he wants is 50 degrees, not 45 or 55.

const int fudgeFactor = 1.5;

1.5 is not an integer.