Schematic: 1 Button and 1 Potentiometer

Sketch: Each time the button is pressed, acquire the value of the potentiometer in a vector of 10 positions and print it on the serial monitor or LCD display.

At the end print the average (sum of values ​​/ 10)

#define BUTTON 5
#define ARRAY_SIZE 10
int counter = 0;
int valori[ARRAY_SIZE];
int pinPotenziometro = A0;
int statoPresenteBottone = LOW; //why so LOW?
float media = 0.0;
void setup() {
  pinMode(BUTTON, INPUT);
  for (int i = 0; i < ARRAY_SIZE; i++) valori[i] = 0;
void loop() {
  if (counter > ARRAY_SIZE) {
    counter = 00;
    media = 0.0; // when we fill array fully - we start again
    for (int i = 0; i < ARRAY_SIZE; i++) valori[i] = 0;
  statoPresenteBottone = digitalRead (BUTTON);
  while (statoPresenteBottone == HIGH) {
    valori[counter] = analogRead(A0);
    media = media + valori[counter];
   Serial.print("Average: ");
   Serial.println(media / counter);
   Serial.print("Button pressed times: ");
    statoPresenteBottone = LOW;

That's what I did but it doesn't work like I want. I need to get the average of the 10 numbers I acquired at the end but it does it each time.

  • At the very least the button would need to be debounced, and you'd likely want to wait until a transition back to LOW before taking a another reading, unless you explicitly want to read as long as the button is held down. Unrelated, but telling us there's a button and a pot isn't a schematic. – Dave Newton May 26 at 20:18
  • I need to change the program like after we acquired the 10 values the program does the average and then restarts from 0 he needs 10 values that u acquire from potentiometer and repeat u understand what i mean? But idk how to fix It – il king dei kings May 26 at 21:08
  • @dave Newton u know how can this be fixable can u modify the sketch pls? D: – il king dei kings May 26 at 21:10
  • @ilkingdeikings Which part in particular is causing a problem? – Dave Newton May 26 at 21:19
  • 1 Button and 1 Potentiometer is not a schematic, it is a partial parts list – jsotola May 26 at 21:34

I will not write the code for you (as explained in my comment to your duplicate question), though I will point out some problems in your code and give hints how to solve them.

Mainly your program structure isn't correct. The first hint is to not look at real C/C++ code in the first step. Write down step by step, what the program should do. Here I'm doing this for you, as I understand your description:

Initialize everything (setup)
  if button was pressed
    loop 10 times
      take analog reading and add it to the sum
      delay 10ms (for example) to keep the reading regularly spaced
    calculate average by dividing the sum of values through 10
    print average over serial

And from this pseudo code you would start to write the corresponding C/C++ code for the Arduino. For the "Initialize everything" you can reuse your current setup() function (assuming you already have a pulldown resistor on your button pin). What I wrote as "loop" would just be the loop() function. The "if button was pressed" will be a if(digitalRead(button_pin)). The 10 analog readings could be written with a for loop, adding the analog values to a local variable and delaying a bit to make the readings regularly spaced in a specific distance (you can change that to your liking. You could also delay way shorter with delayMicroseconds()).

And now some notes for your original code, that you should keep in mind for writing the new one:

  • You have the lines (I omitted some lines, that are irrelevant to my point here):

      statoPresenteBottone = digitalRead (BUTTON);
      while (statoPresenteBottone == HIGH) {
          statoPresenteBottone = LOW;

    Follow the logic here: We read the button state and save it in a variable. Then we loop while this variable is HIGH. But already in the first iteration of that loop we are setting the variable to LOW. That means the while loop will not loop. It will only run once. Also: If you want to check if the button is still HIGH, you actually need to read it. The variable statoPresenteBottone will not change automatically, when you press or release the button. You need to actually read the button pin with digitalRead(BUTTON). The variable is just a small space in memory, where you store data (like the last read state of the button). It is not connected to any hardware. That has to be done by the software.

  • You use an array for the analog values, though in this case you don't need that. For calculating the average it is enough to just add up the values and then divide by the number of measurements. The array is just not needed. And when removing the array, the variable counter and the complete array reset code becomes unnecessary and can be removed.

  • We cannot see your circuit, so please note, that you need a pulldown resistor on the pin, that you connected to the button. Otherwise you would have a floating pin, resulting in a randomly and often rapidly changing pin state. Often the people instead use the internal pullup resistor of the Arduino, by using pinMode(pin, INPUT_PULLUP) and connecting the button to ground instead of Vcc. Though that reverses the logic (LOW then means the button is pressed, HIGH means it is not pressed). It's up to you, want you want to use.

  • In the comments others pointed out button debouncing. That is an important issue, though for the above pseudocode structure that is not that important I think (this depends on how exactly you want the button to behave). So you should keep debouncing in mind. There are many tutorials about that online. I often suggest using the Bounce2 library, since it already handles the button debouncing for you and is fairly easy to use. Though - as said - I don't think that you really need it in this case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.