how to substract first and second reading in arduino

Hi, i want to make arduino understand the system where when the reading after minus reading before equal to 0-0.05 , the measurement is stable.. can i get simple programming. the reading is come from the same sensor. thank you

if ( abs(reading1 - reading2) < 0.05)

I always like to use a pair of low-pass filters for this kind of thing.

A low-pass filter looks like this:

float filteredValue = 0;

void loop() {
  delay(some known delay value);
  int reading = get_reading();
  filteredValue = .75 * filteredValue + .25 * reading;

  if(round(filteredValue) == reading) {
    the_value_is_stable(reading);
  }
}

The filteredValue will converge on the reading at a well-defined rate, governed by your sample speed (the delay) and the damping that you use (.75).

When the filtered value becomes equal to your reading, then the signal has probably been stable.

But there’s always a chance that your unstable reading might just happen to be equal to the filterd value. So use a pair of these filters set to different rates.

float filteredValue1 = 0;
float filteredValue2 = 0;

void loop() {
  delay(some known delay value);
  int reading = get_reading();
  filteredValue1 = .75 * filteredValue1 + .25 * reading;
  filteredValue2 = .99 * filteredValue2 + .01 * reading;

  if(fabs(filteredValue1 - reading) < 1 && fabs(filteredValue2 - reading) < 1) {
    the_value_is_stable(reading);
  }
}

There is always a trade-off: the stronger your filtering, the less responsive the system is. If the input is a little noisy, then these filters may never settle.

You can use less damping. You can also adjust the size of the “window” to detect the signal settling:

  if(fabs(filteredValue1 - reading) < .25 && fabs(filteredValue2 - reading) < 5) {
    the_value_is_stable(reading);
  }

The actual appropriate values depend on your signal. You might also include a timeout so as not to leave it hanging forever - it it times out, light the “reading error” LED.

Low-pass is a great way to strip noise out of a signal, and you only need one static variable (or two, in this case).