I need to implement an algorithm that adapts to the rate of change of a variable. Lets say I have an integer HumidityPercent and an external sensor feeding data on real time. I found several ways to detect if my var has changed:
private float HumChange
{
get { return HumidityPercent; }
set
{
HumidityPercent = value;
if (HumidityPercent != 100)
{
// Do what?
}
}
}
I have a timer (a stopwatch) to calculate milliseconds passed. Thing is: How can I invoke a change in a new variable to store both milliseconds and the new value?
private double newHum = (HumidityPercent, timer.TotalMilliseconds);
But after that how can I differentiate?
Any help would be appreciated.
If you need the rate of change then you need to have three things:
If you have a timer running then the first time it fires all it needs to do is store the humidity.
The next time it fires it needs to move the "current" value into the "previous" value, update the "current" value and do the rate of change calculation.
So if you have your values as nullable doubles you can test to see if you have a previous value or not:
private double? previous;
private double current;
private double rateOfChange;
private void TimerTick(....)
{
if (previous == null)
{
current = GetHumidity();
}
else
{
previous = current;
current = GetHumidity();
rateOfChange = (current - previous) / time;
}
}