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() {
Serial.begin(9600);
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: ");
Serial.println(counter);
statoPresenteBottone = LOW;
counter++;
}
}
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
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 ?
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
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.
Hi,
First you need to do a sample WHEN THE BUTTON IS PRESSED not WHILE THE BUTTON IS PRESSED
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.