Hello I have been trying to measure the average of 10 values at a time that my analog sensor on my arduino uno is reading. My goal is to have it read 10 values and average them and then that triggers a digital port (which I have working correctly). The code I have below stores it as an array but in the Serial Monitor it does not change the values at all in the array at all it just outputs the same numbers.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int Array[numReadings];
int total = 0; // the running total
int average; // the average
void setup() {
analogReference(INTERNAL); //Sets voltage from (0- 1.1V)
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
for (int i=0; i < numReadings; i++){
Array[i] = analogRead(A0);
delay(500);
}
}
void loop() {
for (int i; i < numReadings; i++){
total = total + Array[i];
}
// calculate the average:
average = total / numReadings;
Serial.print(" Average A0: ");
Serial.println(average);
}
Is putting the readings into an array serving some purpose, perhaps just an exercise? You can average them by adding them up as you read them, then when you get 10, divide by 10.
I figured this would be the easiest way to get the average without doing a running average. I did an array of 10 values and calculated the average and then if that value is greater than some number then trigger the port. Then I wanted to loop that.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int Array[numReadings];
int total = 0; // the running total
int average; // the average
void setup() {
analogReference(INTERNAL); //Sets voltage from (0- 1.1V)
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
for (int i=0; i < numReadings; i++){
Array[i] = analogRead(A0);
delay(500);
}
for (int i; i < numReadings; i++){
total = total + Array[i];
}
// calculate the average:
average = total / numReadings;
Serial.print(" Average A0: ");
Serial.println(average);
}
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int Array[numReadings];
int total = 0; // the running total
int average; // the average
void setup() {
analogReference(INTERNAL); //Sets voltage from (0- 1.1V)
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
for (int i; i < numReadings; i++){
Array[i] = analogRead(A0);
delay(500);
}
for (int i; i < numReadings; i++){
total = total + Array[i];
}
// calculate the average:
average = total / numReadings;
Serial.print(" Average A0: ");
Serial.println(average);
}
void loop() {
static unsigned long sum;
static int index;
sum -= Array [index];
sum += Array [index++] = analogRead(A0);
if (index >= numReadings )
index = 0;
average = sum / numReadings;
Serial.print(" Rolling average A0: ");
Serial.println(average);
}
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int Array[numReadings];
int total = 0; // the running total
int average; // the average
void setup() {
analogReference(INTERNAL); //Sets voltage from (0- 1.1V)
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
for (int i=0; i < numReadings; i++){
Array[i] = analogRead(A0);
delay(500);
}
for (int i=0; i < numReadings; i++){
total = total + Array[i];
}
// calculate the average:
average = total / numReadings;
Serial.print(" Average A0: ");
Serial.println(average);
}
I tried this it still did not work and I was advised a smoothing function would be more effective. (I am hesitant to switch methods now until I figure this out