How to evaluate and compare two float every 3 sec.

Hi

Imagine the typical "blink without delay" program", but instead of switching the state of a LED you would like to read the current value of a float.

The value of the float is constantly changing and every 3 sec. i would like to read the current value of the float.

This value is then compared to the last value (the 3 sec old float). We could, for instance, subtract one from the other and get at third value as a result.

This third value is the compared to the next occuring "third value" and so on.

If we see three identical "third values" in a row (meaning that the float has changed in a linear manner for 9 sec) i would like to "do something" with a simple IF statement.

But i'm having a hard time realizing how to save these "third values" into different floats and then overwriting them again once i have spent 3 (or perhaps a bit more) of them.

Hope it all made sense to you guys..

Could you use a short array?

It isn't a good idea to compare floats for equality, better to allow a little "wobble-room" of a small fraction of the possible range of the values. The abs macro is useful here.

you could store the incomming values in a four element array and compare the three differences
when a new value arrives overwrite the oldest data and compare again
be careful comparing floats, it is not like comparing integer whole numbers
real number accuracy depends on the number of bits used to store the floating point data
a technique is to subtract the values, take the absolute value of the result and see if it is less than some ‘error’ value, e.g.

void loop() {
   float x=Serial.parseFloat();
  float y=Serial.parseFloat();
  if(fabs(x-y) < 1.0e-3) Serial.println("identical");
  else                   Serial.println("different");
}

in this case if the difference is less than 1.0e-3 the values are conisdered identical

Thanks for your input.

But how can i store the float's before comparing them? I have to store the value (difference between current value and last value) for some time in different floats, them to compare them.

But how can i store the float's before comparing them?

float saved_x = x;

Store the values in an array.

Will discrete groups of 3 values be tested or will it be done on a rolling test of the 3 most recent values ?

Either way, do the values have to be floats, because of how they are held they are not accurate beyond a couple of decimal places so you may need to ignore less significant decimal places. How many decimal places are significant in your application ? Where do the values come from and is there any chance of them being sent as integers which are easier to compare successfully.

A rolling test of the 3 most recent values, correct.

The values come from a temperature sensor (ds18b20) and i would like two decimal places.

The values come from a temperature sensor (ds18b20)

Integers come from the temperature sensor. Somewhere else, the code is converting those integers to float values.

It is much easier to compare integers than floats.

TolpuddleSartre:
It isn’t a good idea to compare floats for equality, better to allow a little “wobble-room” of a small fraction of the possible range of the values. The abs macro is useful here.

Depends what is generating the floats.

horace:
you could store the incomming values in a four element array and compare the three differences
when a new value arrives overwrite the oldest data and compare again
be careful comparing floats, it is not like comparing integer whole numbers
real number accuracy depends on the number of bits used to store the floating point data
a technique is to subtract the values, take the absolute value of the result and see if it is less than some ‘error’ value, e.g.

void loop() {

float x=Serial.parseFloat();
  float y=Serial.parseFloat();
  if(fabs(x-y) < 1.0e-3) Serial.println(“identical”);
  else                  Serial.println(“different”);
}



in this case if the difference is less than 1.0e-3 the values are conisdered identical

If your floats are very large* you should divide them and compare the order of magnitude.
precondition value <> 0

void loop() {
  float x=Serial.parseFloat();
  float y=Serial.parseFloat();

  if (fabs(x/y - 1) < 1.0e-3) Serial.println("considered identical");
  else                   Serial.println("definitely different");
}

very large*: the exponent is larger than # significant digits of the mantissa