Im trying to use AnalogRead to get voltage data from a seonsr. It works so far. I wanted to determine a mean value of x measuremnts to increase the accuracy. But when I see my results throught Printline I figure out the my result increases all the time. SO my question is is my code wrong or write ?
It is just a few lines to calculate the mean value !!
Thanks in advance
const int numReadings = 100;// how many measurments shall be calculated to the mean value calculation
float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
int Sensor_1 = A0;// Sensor 1
float Offset_Voltage_Sensor_1= 1.635; // Offset Voltage of the sensor must be removed -1.635 to get 0 volt
float volt= 3.300000/1024.000; // 3.3 Volt from Arduino DUE / 2^10 bit for 10 bits resolution
void setup()
{
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
analogReadResolution(10); // Max @ DUE 12 Bit
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = (analogRead(Sensor_1)*0.003223-Offset_Voltage_Sensor_1);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
// ...wrap around to the beginning:
readIndex = 0;
// calculate the average:
average = total / numReadings;
Serial.print(" Voltage Sensor 1 : ");
Serial.println( average,4);
delay(100); // delay in between reads for stability
}
You never Zeroed your total after the averaging. Also, it should be better if you use a 'for loop' instead of if>= and ++.
Here your corrected code:
const int numReadings = 100;// how many measurments shall be calculated to the mean value calculation
float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
int Sensor_1 = A0;// Sensor 1
float Offset_Voltage_Sensor_1= 1.635; // Offset Voltage of the sensor must be removed -1.635 to get 0 volt
float volt= 3.300000/1024.000; // 3.3 Volt from Arduino DUE / 2^10 bit for 10 bits resolution
void setup()
{
Serial.begin(9600);
// initialize all the readings to 0:
for (readIndex = 0; readIndex < numReadings; readIndex++)
readings[readIndex] = 0;
}
void loop() {
analogReadResolution(10); // Max @ DUE 12 Bit
readings[readIndex] = (analogRead(Sensor_1)*0.003223-Offset_Voltage_Sensor_1);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
{
average = total / numReadings;
Serial.print(" Voltage Sensor 1 : ");
Serial.println( average,4);
delay(100); // delay in between reads for stability
readIndex = 0;
total = 0;
}
}
Here a code with using 'for loop':
const int numReadings = 100;// how many measurments shall be calculated to the mean value calculation
float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
int Sensor_1 = A0;// Sensor 1
float Offset_Voltage_Sensor_1= 1.635; // Offset Voltage of the sensor must be removed -1.635 to get 0 volt
void setup()
{
Serial.begin(9600);
// initialize all the readings to 0:
for (readIndex = 0; readIndex < numReadings; readIndex++)
readings[readIndex] = 0;
}
void loop() {
analogReadResolution(10); // Max @ DUE 12 Bit
for(readIndex=0; readIndex<numReadings;readIndex++)
{
readings[readIndex] = (analogRead(Sensor_1)*0.003223-Offset_Voltage_Sensor_1);
total = total + readings[readIndex];
}
average = total / numReadings;
Serial.print(" Voltage Sensor 1 : ");
Serial.println( average,4);
delay(100); // delay in between reads for stability
total = 0;
}