Jittery map() values

I'm using the values from a potentiometer and mapping it to 0-255 with very jittery results.

ie.

int Constrainedaverage = constrain(potmovingavg,0,1000);
  Pot1MappedOutputValue = map(Constrainedaverage, 5, 1000, 0, 255);

when the average value read from pot is certain values, for example 550 the map value fluctuates between 139 & 140

pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192

and with
int Constrainedaverage = constrain(potmovingavg,0,999);
Pot1MappedOutputValue = map(Constrainedaverage, 5, 999, 0, 255);

the map value fluctuates wildly as 197 & 196

pot1RAWValue : 550Constrainedaverage : 550 / potmovingavg: 550 / Pot1MappedOutputValue: 139
pot1RAWValue : 551Constrainedaverage : 551 / potmovingavg: 551 / Pot1MappedOutputValue: 140
pot1RAWValue : 550Constrainedaverage : 550 / potmovingavg: 550 / Pot1MappedOutputValue: 139
pot1RAWValue : 551Constrainedaverage : 551 / potmovingavg: 551 / Pot1MappedOutputValue: 140

I tried understanding what the issue might be from here:

but exceeded my capacity. Any takers?

Full code below:

//#include "FastLED.h"                                          // FastLED library.
#include <movingAvg.h> 

////////////////////////////////////////////POTENTIOMETER1 / BRIGHTNESS
int pot1 = A5;
int pot1RAWValue  = 0;
int pot1OldRAWValue  = 0;
const int numReadings1 = 10;
int readings1[numReadings1];      // the readings from the analog input
int index1 = 0;                  // the index of the current reading
int total1 = 0;                  // the running total
int average1 = 0;                // the average
int Pot1MappedOutputValue = 32;
int oldPot1MappedOutputValue = 0;
movingAvg potentiometeraverage(25);   
////////////////////////////////////////////POTENTIOMETER1////////////////////////////////////////////






////////////////////////////////////////////////SETUP/////////////////////////////////////////////////

void setup() {
  Serial.begin(57600);
 potentiometeraverage.begin();




  
}////////////////////////////////////////////////SETUP/////////////////////////////////////////////////


////////////////////////////////////////////////LOOP/////////////////////////////////////////////////
void loop() {


   /////////////////////////////////////////////// TGAN_Potentiometer_1
  pot1RAWValue  = analogRead(pot1);  // Serial.print("pot1RAWValue : "); Serial.println(pot1RAWValue );

//Discard the value if its less that +2 of the old value. Removes jittery readings.
 if (abs(pot1RAWValue-pot1OldRAWValue)<= 3) 
   {
   pot1RAWValue = pot1OldRAWValue;
  }
   


 
 int potmovingavg = potentiometeraverage.reading(pot1RAWValue); 
   
int Constrainedaverage = constrain(potmovingavg,0,1000);
  Pot1MappedOutputValue = map(Constrainedaverage, 5, 1000, 0, 255);


 
  if (Pot1MappedOutputValue != oldPot1MappedOutputValue) 
  {
    Serial.print("pot1RAWValue : "); Serial.print(pot1RAWValue );  Serial.print(" / Constrainedaverage : "); Serial.print(Constrainedaverage ); Serial.print(" / potmovingavg: "); Serial.print(potmovingavg); Serial.print(" / Pot1MappedOutputValue: "); Serial.println(Pot1MappedOutputValue);
  }
  oldPot1MappedOutputValue = Pot1MappedOutputValue;
  /////////////////////////////////////// TGAN_Potentiometer_1 ///////////////////////////////////////

}

I think the values might be expected to vary within a narrow band of values. If you're expecting a stable, unique, value at a constant input value, that's likely not going to happen. Perhaps you could take 25 sample values and display the average or something like that, to make the output appear more stable.

Deriving a stable set of discrete values covering the full range 0 to 255 from a 270 degree sweep potentiometer is pushing it a bit.
Have a look here though: Hysteresis - Introductory Tutorials - Arduino Forum.

the map value fluctuates wildly as 197 & 196

This is not "wild fluctuation".

For example, if a more accurate representation of the true value would be 196.5, then that is perfectly acceptable measurement noise, and is completely unavoidable.

One popular approach to reducing measurement noise is to average some values, as suggested above.

I believe I didnt initially pose the question correctly but in any case I did try averaging the final mapped value too and not much changed.

The issue is that the potentiometer at some positions reads jittery values. ie the value fluctuates between 2 incremental values and this causes a lot of issue in final values whether averaged, mapped or not.

I placed a 0.1uf capacitor between ground and input pin of pot and still nothing changed.

pot1RAWValue : 397 / Constrainedaverage : 396 / potmovingavg: 396 / Pot1MappedOutputValue: 100 / Pot1MappedMovingAverage: 100
pot1RAWValue : 396 / Constrainedaverage : 397 / potmovingavg: 397 / Pot1MappedOutputValue: 101 / Pot1MappedMovingAverage: 101
pot1RAWValue : 396 / Constrainedaverage : 396 / potmovingavg: 396 / Pot1MappedOutputValue: 100 / Pot1MappedMovingAverage: 100
pot1RAWValue : 396 / Constrainedaverage : 397 / potmovingavg: 397 / Pot1MappedOutputValue: 101 / Pot1MappedMovingAverage: 101
pot1RAWValue : 397 / Constrainedaverage : 396 / potmovingavg: 396 / Pot1MappedOutputValue: 100 / Pot1MappedMovingAverage: 100
pot1RAWValue : 396 / Constrainedaverage : 397 / potmovingavg: 397 / Pot1MappedOutputValue: 101 / Pot1MappedMovingAverage: 101
pot1RAWValue : 397 / Constrainedaverage : 396 / potmovingavg: 396 / Pot1MappedOutputValue: 100 / Pot1MappedMovingAverage: 100

I think scrapping any pot raw value if the value is not more than 3 from the previous read value solves it. crude but hopefully does what is needed.

Final code:

//#include "FastLED.h"                                          // FastLED library.


////////////////////////////////////////////POTENTIOMETER1 / BRIGHTNESS
#include <movingAvg.h> 
int pot1 = A5;
int pot1RAWValue  = 0;
int pot1OldRAWValue = 0;
int Pot1MappedOutputValue = 32;
movingAvg Pot1MappedAverage(10);
int oldPot1MappedMovingAverage;
movingAvg potentiometeraverage(25);   
////////////////////////////////////////////POTENTIOMETER1////////////////////////////////////////////



////////////////////////////////////////////////SETUP/////////////////////////////////////////////////
void setup() {
  Serial.begin(57600);
 potentiometeraverage.begin();
 Pot1MappedAverage.begin();
}////////////////////////////////////////////////SETUP/////////////////////////////////////////////////


////////////////////////////////////////////////LOOP/////////////////////////////////////////////////
void loop() {
   /////////////////////////////////////////////// TGAN_Potentiometer_1
  pot1RAWValue  = analogRead(pot1);  // Serial.print("pot1RAWValue : "); Serial.println(pot1RAWValue );

 if (abs(pot1RAWValue-pot1OldRAWValue)<= 3) 
   {
   pot1RAWValue = pot1OldRAWValue;
  }


int potmovingavg = potentiometeraverage.reading(pot1RAWValue); 
int Constrainedaverage = constrain(potmovingavg,0,1000);
Pot1MappedOutputValue = map(Constrainedaverage, 0, 1000, 0, 255);
int Pot1MappedMovingAverage = Pot1MappedAverage.reading(Pot1MappedOutputValue);
  if (Pot1MappedMovingAverage != oldPot1MappedMovingAverage) 
  {
    Serial.print("pot1RAWValue : "); Serial.print(pot1RAWValue );  Serial.print(" / Constrainedaverage : "); Serial.print(Constrainedaverage ); Serial.print(" / potmovingavg: "); Serial.print(potmovingavg); Serial.print(" / Pot1MappedOutputValue: "); Serial.print(Pot1MappedOutputValue); Serial.print(" / Pot1MappedMovingAverage: "); Serial.println(Pot1MappedMovingAverage);
  }
  oldPot1MappedMovingAverage = Pot1MappedMovingAverage;
  /////////////////////////////////////// TGAN_Potentiometer_1 ///////////////////////////////////////

pot1OldRAWValue = pot1RAWValue;
}