Looking for a little help fine tuning a pot reading

hi all. I have a question and looking for suggestions on a problem i'm trying to work out. I have a 10k pot connected to my nano so i can get a value to control an air pump. here's a snippit of the code i'm trying to work out.

void loop(){

  int pumpReading = analogRead(pumpPin);                                                      
  long pumpPeriod = map(pumpReading, 1023, 0, minPump, maxPump);         

newPumpPeriod = pumpPeriod;

if  (newPumpPeriod != oldPumpPeriod)        {  monitorPressure();  }

//other code

oldPumpPeriod = pumpPeriod;

}    end of loop

Ok. here's my issue. the pumpPeriod can vary + or - 1 every now and then on it's own. It's not a fixed value because it's connected to a pot. and i'm trying to get the monitorPressure() to run whenever the value of the pumpPeriod is adjusted more than 2 points instead of just seeing if it's changed or not. Right now the pumpPeriod resolution is between 200 and 800. And those numbers can't be changed so i can't lower them to make the reading more stable.

So i'm unsure how i can go about using if statements or logical statements or whatever i need to have a 2 point difference between the values before it runs the monitorPressure() function.

Any help is appreciated.

Save the old reading and if the difference between old and new is greater than 2, use the new value. Otherwise ignore the change.

Yes. That's what i'm trying to do. But what confuses me is normally i would try to subtract the 2 values together. which works fine when going from a lower value to a higher value. but if i try to go from a higher value to low value, it would be a negative number and didn't know how to treat it. Or if i'm going about it all wrong.

Google

  arduino hysteresis

maybe a new word to your vocabulary.

Also, you need to remember absolute value. If the absolute value of the difference between two readings is large enough &c.

a7

if ( abs(new-old) > 2 )

something like that?

Thanks for the help. I quickly uploaded it and tested it real quick and it worked. I've never had to use abs() before and figured there was an easy way to do it, but didn't understand it.