Having trouble with an if statement

WizenedEE:

void loop(){

pot = analogRead(A1);

// Do your stuff here
  int potdiff = pot - pot_before;
  potdiff = abs(potdiff);
  if (potdiff > 2) {
    //do what you want to do here if the difference is bigger than 2
  }

pot_before = pot;
}

That code is broken as it will never notice slow movement of the wiper when the difference between successive samples is less than 2 - you mustn't update pot_before unless you've noticed a significant change.

#define THRESHOLD 2          // #define the threshold for easy code maintainance
int pot_before = -2*THRESHOLD ; // initialize with a value that forces the if true first time round

void loop()
{
  int pot = analogRead(A1);

  // Do your stuff here
  if (abs (pot - pot_before) > THRESHOLD) {
    //do what you want to do here if the difference is bigger than the THRESHOLD

    pot_before = pot ;
  }

}