1 Button and 1 pontetiometer

Sketch: Each time the button is pressed, acquire the value of the potentiometer in a array of 10 positions and at the end he does the average of 10 values & print it on the serial monitor.

#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() {
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;

Ive made this guys but It doesnt work as It should , thats why in asking here if there is any expert that could help me im trying since Yesterday but nothing? If u can guys u can edit the sketch and make It work as It should? Basically i want 10 button presses with one potentiometer reading per press? And then after the tenth press, to calculate the average and display it?

If u can edit it for me pls, i dont need explaining cus i know the explaination but still aint working for me

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

I dont get errors but the problem Is that he does the average at every Number he acquired

(Would this be a good place to present the average?)

What's the array for?
You just need a running total.

U can check here my project

The array is for the 10 values i acquire from potentiometer

No, I can't.
I don't have/want a TinkerCAD account
(I think I need that phrase as a keyboard macro)

Is that part of the assignment specification, or is that how you thought it should be done?

But do you need the individual values for anything apart from calculating the average ?

Why not zero the total then read 10 values, adding them to the total each time, and when you have 10 calculate the average by dividing the total by 10 ?

Nope matter thing Is that he works xD

I need the values to do the average and then the variance but for now in trying to solve the first thing when thats done i will think about how to do the variance

well u dont need an account anyways ;D

When the button is pressed shortly (press it less than 100ms), then it works.
What is the problem ?

This is a Tinkercad screendump:

And this is a Wokwi screendump:

Your Wokwi project is this link:

No account required. Logging in is only required to store a project. Press the start-button in the middle-upper of the screen.

Wokwi is still in development. In my opinion it has already passed Tinkercad, because a library can be added as local files. Here is an example with the BigNumber library that I added as local files to the project.

Well, how am I supposed to see your project?
(sp. "you")

There is a difference, you want to press the button and sample ONCE, then press again and sample AGAIN, until you have 10 readings to calculate the average.

Tom... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.