Reducing pot flickering with code based solutions?

I have some pots hooked up to an Arduino Micro. I’m running it at 3.3v. Its hooke dup to a program that adjusts colour on screen, I’m getting some noise, mostly flickering , any suggestions on what I can do in code to help with this? It seems to only bugger up when it has keypresses. I can’t open the box to add resistors and things at this point, so code suggestions please:

Thank you!

Here’s what I have:

// first knob (contrast)
int contrast_val = 0;
int contrast_prev;
const int contrast_pin = A5;

// second knob (dither)
int dither_val = 0;
int dither_prev;
const int dither_pin = A4;

// third knob palette
int palette_val = 0;
int palette_prev = 0;
const int palette_pin = A3;


void setup() { 
  Serial.begin(9600);  // initialize serial communication at 9600 bits per second:
  Keyboard.begin();
}


void loop() {

 // contrast logic 

  contrast_prev = contrast_val;
  contrast_val = analogRead(contrast_pin);  // read the input on analog pin 0:
  contrast_val = map(contrast_val, 0, 1023, 0, 100); // map the vlaues to new values

  
  if (contrast_val > contrast_prev) {

    Serial.println("contrast up");
    Keyboard.press('l');
    Keyboard.releaseAll();
    
    
  }else if (contrast_val < contrast_prev){
    Serial.println("contrast down");
    Keyboard.press('h');
    Keyboard.releaseAll();
   
    
  }
  
  // dither set up
  
 dither_prev = dither_val;
 dither_val = analogRead(dither_pin);
 dither_val = map(dither_val, 0, 1023, 0, 100); 

  if (dither_val > dither_prev) {
    Serial.println("dither up");
    Keyboard.press('t');
    Keyboard.releaseAll();
  }else if (dither_val < dither_prev) {

    Serial.println("dither down");
    Keyboard.press('q');
    Keyboard.releaseAll();
  }
  
  
   // palette
  
 palette_prev = palette_val;
 palette_val = analogRead(palette_pin);
 palette_val = map(palette_val, 0, 1023, 0, 100);
                                         
  
  if (palette_val > palette_prev) {
    Serial.println("palette up");
    Keyboard.press('m');
    Keyboard.releaseAll();
  }else if (palette_val < palette_prev) {

    Serial.println("palette down");
    Keyboard.press('j');
    Keyboard.releaseAll();
  }
  
  
  delay(100); // delay by a second.

}

Take a dozen readings or so and select the modal value? (The mode is a particular
kind of average, its the value that occurs most often). If all values read are distinct,
simply try again until it has settled down…

MarkT: Take a dozen readings or so and select the modal value? (The mode is a particular kind of average, its the value that occurs most often). If all values read are distinct, simply try again until it has settled down...

Thanks for the idea, I'm not sure how to do that, I've never done it before. Do you have a link or a tutorial, or an example please?

Read N readings into an array of size N, then count how often the first reading is duplicated,
then the 2nd, etc etc until you’ve found the most popular value. This sort of thing is done
by keeping a running count of the best so far, whenever you find a better count, update the
state, at the end you know you have the best (or one of the best).

MarkT: Read N readings into an array of size N, then count how often the first reading is duplicated, then the 2nd, etc etc until you've found the most popular value. This sort of thing is done by keeping a running count of the best so far, whenever you find a better count, update the state, at the end you know you have the best (or one of the best).

Sudo Code? Sorry. I know I'm bothersome.

missing_n: It seems to only bugger up when it has keypresses. I can't open the box to add resistors and things at this point

Sounds like an electrical problem - can you figure out the cause? What are the switches switching? The better you understand the problem, the better placed you will be to fix it. The ideal solution would be to fix the underlying cause (electrical smoothing, correct pull-down resistor rating, or whatever. But if you can confirm that the problem is associated with keys then for example you could choose to ignore readings sufficiently close to a keypress.

Hmm. Yeah its maybe electrical. The raw reading values flicker at points. So for example you are sitting on a value of '200' it will go '200, 200, 199, 200, 200,200, 199' etc. I'll see what I can do under the hood, as I can take it apart now (couldn't a month or so ago). and I'll try doing that array / sample thing. Sorry for the really late reply, just had to shelve this for a while.

I had a pot that worked like that. Turns out it was a poor choice of value and a 10k linear worked fine. In the meantime I fixed it by ignoring readings that hadn't changed more than two units. A solution that may not suit all projects.