So I was trying to read the output a potenciometer gives a hundred times and to get the arithmetic mean of that hundred outputs shown by the serial comm. in my computer, but it doesn't seem to calculate the mean. I can't find the error, please help me.. ![]()
int x=0;
void setup() {
Serial.begin(9600);
}
void loop() {
int values[x];
if(x<100) {
x++;
values[x]=analogRead(A0);
delay(10);
};
Serial.print("The ");
Serial.print(x);
Serial.print(" time this pot has a value of ");
Serial.println(values[x]);
int y;
unsigned long sum=0;
for(y=0; y<=x; y++) {
sum=(sum + values[y]);
};
long mean=sum / 101;
if (x == 100) {
Serial.print("The mean is ");
Serial.print(mean);
delay(10000);
x=0;
};
}
In the serial monitor it shows this up:
The 1 time this pot has a value of 569
The 2 time this pot has a value of 569
The 3 time this pot has a value of 570
The 4 time this pot has a value of 570
The 5 time this pot has a value of 569
The 6 time this pot has a value of 570
The 7 time this pot has a value of 570
The 8 time this pot has a value of 570
The 9 time this pot has a value of 570
The 10 time this pot has a value of 570
The 11 time this pot has a value of 570
The 12 time this pot has a value of 570
The 13 time this pot has a value of 570
The 14 time this pot has a value of 570
The 15 time this pot has a value of 569
The 16 time this pot has a value of 570
The 17 time this pot has a value of 570
The 18 time this pot has a value of 569
The 19 time this pot has a value of 570
The 20 time this pot has a value of 570
The 21 time this pot has a value of 570
The 22 time this pot has a value of 570
The 23 time this pot has a value of 569
The 24 time this pot has a value of 570
The 25 time this pot has a value of 570
The 26 time this pot has a value of 570
The 27 time this pot has a value of 570
The 28 time this pot has a value of 570
The 29 time this pot has a value of 570
The 30 time this pot has a value of 570
The 31 time this pot has a value of 570
The 32 time this pot has a value of 570
The 33 time this pot has a value of 570
The 34 time this pot has a value of 570
The 35 time this pot has a value of 570
The 36 time this pot has a value of 570
The 37 time this pot has a value of 570
The 38 time this pot has a value of 570
The 39 time this pot has a value of 570
The 40 time this pot has a value of 570
The 41 time this pot has a value of 570
The 42 time this pot has a value of 570
The 43 time this pot has a value of 570
The 44 time this pot has a value of 570
The 45 time this pot has a value of 570
The 46 time this pot has a value of 570
The 47 time this pot has a value of 570
The 48 time this pot has a value of 570
The 49 time this pot has a value of 570
The 50 time this pot has a value of 569
The 51 time this pot has a value of 570
The 52 time this pot has a value of 570
The 53 time this pot has a value of 570
The 54 time this pot has a value of 570
The 55 time this pot has a value of 569
The 56 time this pot has a value of 570
The 57 time this pot has a value of 570
The 58 time this pot has a value of 570
The 59 time this pot has a value of 570
The 60 time this pot has a value of 569
The 61 time this pot has a value of 570
The 62 time this pot has a value of 569
The 63 time this pot has a value of 570
The 64 time this pot has a value of 570
The 65 time this pot has a value of 569
The 66 time this pot has a value of 570
The 67 time this pot has a value of 570
The 68 time this pot has a value of 569
The 69 time this pot has a value of 570
The 70 time this pot has a value of 569
The 71 time this pot has a value of 569
The 72 time this pot has a value of 570
The 73 time this pot has a value of 569
The 74 time this pot has a value of 569
The 75 time this pot has a value of 569
The 76 time this pot has a value of 570
The 77 time this pot has a value of 569
The 78 time this pot has a value of 570
The 79 time this pot has a value of 570
The 80 time this pot has a value of 570
The 81 time this pot has a value of 570
The 82 time this pot has a value of 569
The 83 time this pot has a value of 570
The 84 time this pot has a value of 570
The 85 time this pot has a value of 570
The 86 time this pot has a value of 570
The 87 time this pot has a value of 569
The 88 time this pot has a value of 570
The 89 time this pot has a value of 570
The 90 time this pot has a value of 570
The 91 time this pot has a value of 570
The 92 time this pot has a value of 569
The 93 time this pot has a value of 570
The 94 time this pot has a value of 570
The 95 time this pot has a value of 570
The 96 time this pot has a value of 570
The 97 time this pot has a value of 569
The 98 time this pot has a value of 570
The 99 time this pot has a value of 570
The 100 time this pot has a value of 570
The mean is 0
I don't know why does this happen, maybe you could lend me a hand ![]()