my potentiometer values act slow above 50 % , and slow entire sketch

hello im on a learning path for arduino and C , i have this sketch where i have a switch where i do tap tempo and return values in ms (its working) plus i add a pot to override (override not implemented yet) and change the values in ms , as presented in low settings on pot the display have a really nice update of values BUT when i increase the values the display apears to become more and more laggy until is unable to adjust the resolution i want, i use millis funtction on some part of code but im getting a hard time loocking for the reason of this misbehave ,

#include<TM1637Display.h>
#define CLK 3
#define DIO 4
TM1637Display display(CLK, DIO);
const int NUM_SAMPLES = 20; //array to hold values to be averaged from adc pot
int potSamples[NUM_SAMPLES];
int potIndex = 0;
int totalPotValue = 0;
int lastAveragePotValue = 0;
const int CHANGE_THRESHOLD = 3; //var detection treshold


void setup() {
  display.setBrightness(0xf);
  pinMode (A1, INPUT);     // adc input pot
  pinMode (12, INPUT);     //tap switch input
  pinMode (2, OUTPUT);     //led tempo output
  Serial.begin(9600);      //serial output debug only
  for (int i = 0; i < NUM_SAMPLES; i++) potSamples[i] = 0; // fill array whit 0
}

unsigned long potvalue = analogRead(A1);


int lastTapState = LOW;
unsigned long currentTimer[2] = { 500, 500 };  //array initial tempo
unsigned long timeoutTime = 0;                 //this is when the timer will trigger next
unsigned long indicatorTimeout;                //led blink indicator
unsigned long elapsedTime = 0;                 //var for display ms



void loop() {

  potvalue =  analogRead(A1);
  totalPotValue -= potSamples[potIndex]; //potsamples is the oldest data in array
  totalPotValue += potvalue;
  potSamples[potIndex] = potvalue;
  potIndex++;
  if (potIndex > NUM_SAMPLES - 1) potIndex = 0;

  // Calculate the average pot value
  int averagePotValue = totalPotValue / NUM_SAMPLES;
  int change = abs(averagePotValue - lastAveragePotValue);
  if (change >= CHANGE_THRESHOLD) {
    lastAveragePotValue = averagePotValue;
    currentTimer[0] = averagePotValue;
    currentTimer[1] = averagePotValue;
  }



  int tapState = digitalRead(12);
  if ( tapState == LOW && tapState != lastTapState )   //read pin 12 for transition hi/lo and register when first pressed down
  {
    tap();                                            //call for tap function

  }

  display.showNumberDec(elapsedTime);





  lastTapState = tapState;                           //keep track of the state
  if (millis() >= timeoutTime )

  {
    indicatorTimeout = millis() + 30;                //set the time for led goes off
    rescheduleTimer();
  }
  digitalWrite ( 13, tapState );                    //display the switch state

  if (millis() < indicatorTimeout )                  //display the blink led tempo
  {
    digitalWrite (2, HIGH);
  } else
  {
    digitalWrite (2, LOW );
  }

}


unsigned long lastTap = 0;                            //when the last tap happenned
void tap()                                            //tap funtion
{
  currentTimer[1] = currentTimer[0];                 //keep two var to average later
  currentTimer[0] = millis() - lastTap ;
  lastTap = millis();
  timeoutTime = 0;                                   // force the trigger to happen immediately , sync and blink

}

void rescheduleTimer()                               //set the timer to go off again when reaches the timeout, is the averaged time
{
  timeoutTime = millis() + ((currentTimer[0] + currentTimer[1]) / 2 );  //set timeout value averaged time
  elapsedTime = ((currentTimer[0] + currentTimer[1]) / 2 );     //average display time

}

i just tested separated sketch for each functions tap and pot and they works nice alone , but together they acting strange , could be too much math for 328p?
thanks

or one could sugest a better aproach too
thanks

yep on arduino mega is the same history , tested on another uno board whit same results

BUT when i increase the values the display apears to become more and more laggy until is unable to adjust the resolution i want

You mean you turn the pot and it slows down? Does it speed up if you turn the pot back the other way? Does it slow down if you don't turn it?

No obvious cause to me.

Personally, I would not be writing to the display every time through the loop like you are doing.

You could put in a time check, to only update the display every 100ms or more. Or, you could store the last time, and only update the display if the time has changed. Or do both.

yes as i turn the pot to high values the rate of updates on display slow down , if i turn to low values like 100 or 60 ms the rate of updates turn smooth again , apears to be some link between the time and refresh rate , as i said before , i divided the sketch and test only the function from the pot and he works allrigth even at high values ( good refresh display and response from function)

i will try rewrite the all thing maybe some variables are doing some nasty thing , i cant believe the display in loop can cause this and set the display to only show fraction of 100ms its not a good option as i want a good range for setting time ex, 84 113 230 etc

thanks for reply guys

collingtech:
yes as i turn the pot to high values the rate of updates on display slow down , if i turn to low values like 100 or 60 ms the rate of updates turn smooth again , apears to be some link between the time and refresh rate , as i said before , i divided the sketch and test only the function from the pot and he works allrigth even at high values ( good refresh display and response from function)

i will try rewrite the all thing maybe some variables are doing some nasty thing , i cant believe the display in loop can cause this and set the display to only show fraction of 100ms its not a good option as i want a good range for setting time ex, 84 113 230 etc

thanks for reply guys

You can show values like 84ms, or 15ms, or whatever, but you should only be updating the display every, say, 100ms. Our eyes cannot read much faster than that anyway (hence 24fps for film), and it stops you overloading the display writer. So, every 100ms, you write the number 15ms (or 84ms, etc.) to the display.

What happens if you temporarily change this line

display.showNumberDec(elapsedTime);

to

static byte xx = 0;
display.showNumberDec(xx);
xx += 1;
if (xx > 9) {
   xx = 0;
}

The idea is to limit it to show a single changing digit

If that shows a difference then the advice of others to show the data less frequently should solve the problem.

I note that you are averaging 20 samples. I doubt if it would make any difference if you only use 16 samples and dividing by 16 (a power of 2) will be much faster.

...R

ok , i try several things and to resume the opera the conclusion is the pot function is in loop until the values are done (averaged) who cause the slugish feel i ended up putting a print inside the loop too to show values when the pot is used , apears to be working fine , now i can tap value and override whit pot whit a smooth feel, i raise the treshold to 15 i still gett some jitter from pot but as the thing is protoboard can indeed raise noise

next step is resolve a better solution overall, will try to arrange a better code for this

#define DIO 4
TM1637Display display(CLK, DIO);
const int NUM_SAMPLES = 16; //array to hold values to be averaged from adc pot
int potSamples[NUM_SAMPLES];
int potIndex = 0;
int totalPotValue = 0;
int lastAveragePotValue = 0;
const int CHANGE_THRESHOLD = 15; //var detection treshold


void setup() {
  display.setBrightness(0xf);
  pinMode (A1, INPUT);     // adc input pot
  pinMode (12, INPUT);     //tap switch input
  pinMode (2, OUTPUT);     //led tempo output
  Serial.begin(9600);      //serial output debug only
  for (int i = 0; i < NUM_SAMPLES; i++) potSamples[i] = 0; // fill array whit 0
}

unsigned long potvalue = analogRead(A1);


int lastTapState = LOW;
unsigned long currentTimer[2] = { 500, 500 };  //array initial tempo
unsigned long timeoutTime = 0;                 //this is when the timer will trigger next
unsigned long indicatorTimeout;                //led blink indicator
unsigned long elapsedTime = 0;                 //var for display ms



void loop() {

  potvalue =  analogRead(A1);
   potvalue = map (potvalue, 0, 1023, 60, 880);
  totalPotValue -= potSamples[potIndex]; //potsamples is the oldest data in array
  totalPotValue += potvalue;
  potSamples[potIndex] = potvalue;
  potIndex++;
  if (potIndex > NUM_SAMPLES - 1) potIndex = 0;

  // Calculate the average pot value
  int averagePotValue = totalPotValue / NUM_SAMPLES;
  int change = abs(averagePotValue - lastAveragePotValue);
  if (change >= CHANGE_THRESHOLD) {
    lastAveragePotValue = averagePotValue;
    currentTimer[0] = averagePotValue;
    currentTimer[1] = averagePotValue;
    
 display.showNumberDec(averagePotValue);
  }



  int tapState = digitalRead(12);
  if ( tapState == LOW && tapState != lastTapState )   //read pin 12 for transition hi/lo and register when first pressed down
  {
    tap();                                            //call for tap function

  }

 //display.showNumberDec(elapsedTime);





  lastTapState = tapState;                           //keep track of the state
  if (millis() >= timeoutTime )

  {
    indicatorTimeout = millis() + 30;                //set the time for led goes off
    rescheduleTimer();
  }
  digitalWrite ( 13, tapState );                    //display the switch state

  if (millis() < indicatorTimeout )                  //display the blink led tempo
  {
    digitalWrite (2, HIGH);
  } else
  {
    digitalWrite (2, LOW );
  }

}


unsigned long lastTap = 0;                            //when the last tap happenned
void tap()                                            //tap funtion
{
  currentTimer[1] = currentTimer[0];                 //keep two var to average later
  currentTimer[0] = millis() - lastTap ;
  lastTap = millis();
  timeoutTime = 0;                                   // force the trigger to happen immediately , sync and blink
 //display.showNumberDec();
}

void rescheduleTimer()                               //set the timer to go off again when reaches the timeout, is the averaged time
{
  timeoutTime = millis() + ((currentTimer[0] + currentTimer[1]) / 2 );  //set timeout value averaged time
  elapsedTime = ((currentTimer[0] + currentTimer[1]) / 2 );     //average display time

 display.showNumberDec(elapsedTime);
}
for (int i = 0; i < NUM_SAMPLES; i++) potSamples[i] = 0; // fill array whit 0

They’re already zero - no need for belt and braces.

Don’t forget larger numbers take longer to print.

so i can delete this line ? its redudant ?

thanks for great advices guys

Any global data item is initialised to zero unless it is explicitly initialised to some other value.

thanks AWOL

another minor issue is when tap function is pressed (you need to press twice to set tempo) he send unwanted values when switch is pressed , seems he send values currentTimer[0] and currentTimer[1] before send a averaged value ,

there is a way to only send the value after averaged ?