Hello,
I am trying to enter first ten values in an array and calculate their average.
I am able to print Pdelta values correctly, but it does not calculate average and prints average as 0.
Can anyone please tell me where am I going wrong.
Hello,
I am trying to enter first ten values in an array and calculate their average.
I am able to print Pdelta values correctly, but it does not calculate average and prints average as 0.
Can anyone please tell me where am I going wrong.
what do you think are the values you put in your array?
For one thing you need a Serial.Begin() in setup().
Since your "values" depend on PDelta, which depend on max and min, which are not set to anything, I am not surprised you get 0.
Since your "values" depend on PDelta, which depend on max and min, which are not set to anything, I am not surprised you get 0.
I am able to print max and min values and Pdelta.
The code I posted is just for entering Pdelta values to the array and calculating average.
what do you think are the values you put in your array?
Pdelta values
you give us a code that does not work, where max, min, Pdelta are empty etc...
don't post snippets... how can we help otherwise?
Well, assuming the code is correct, you always print the average even if you have less than 10 readings.
But post all the code!
I don't understand this bit
for (int i = 0; i < numReadings ; i++)
{
total += readings[i];
readings[i] = value;
}
Sorry,
The line was commented out and I missed it while posting the code .
for (int i = 0; i < numReadings ; i++)
{
total += readings[i];
// readings[i] = value;
}
[\code]
use statistics.h
#include <Statistics.h>
Statistics stats(10);
void setup()
{
Serial.begin(9600);
}
void loop()
{
int data = analogRead(A0);
stats.addData(data);
Serial.print("Mean: ");
Serial.print(stats.mean());
Serial.print(" Std Dev: ");
Serial.println(stats.stdDeviation());
}
dpoornima:
I am able to print max and min values and Pdelta.The code I posted is just for entering Pdelta values to the array and calculating average.
what do you think are the values you put in your array?Pdelta values
Those float values are you storing in an integer array. That spell trouble.
We have no idea how big or small your values are.
How do I correct it so that it waits for 10 values and then prints the average?
Do you really want to wait for 10 values? Or, do you not want to print anything until 10 or more values have been read?
index tells you how many values you have read. You can use the value in index to determine whether or not to calculate and print the average.
here is an example to study
you have a rolling buffer of 10 (maxValues) entries
you want to do an average on the last 5 (nbValuesForAverage) values at most (if you have less, then on less)
this example could be simplified - I tried to make it easy to read so that all the different cases are spelled out (some could be grouped with some maths, min max and modulo).
here is some code to see what's going on, set the serial console at 115200 bauds, put a floating wire into A0 so that you get some kind of random noise for your "sensor values" and run it. It will capture a new value every second, store it into the array and display the 5 indexes it uses to calculate the average and the average.
const byte sensorPin = A0; // whatever
const int maxValues = 10; // the number of data points to remember
unsigned int valuesOfInterest[maxValues]; // the data, circular buffer
const int nbValuesForAverage = 5; // we only care about the few last ones
int nextIndex = 0; // this is where to store the next data point
boolean arrayIsFull = false; // this helps handle the initial stage when array is not full yet
double printAverageValue()
{
unsigned long int sum = 0;
double avg;
byte startAverageSumAt;
Serial.println(F("-----------------"));
if (arrayIsFull) {
// decide where to start counting from
if (nextIndex - nbValuesForAverage >= 0) {
startAverageSumAt = nextIndex - nbValuesForAverage;
} else {
startAverageSumAt = maxValues + nextIndex - nbValuesForAverage;
}
int cnt = 0;
int i = startAverageSumAt;
do {
Serial.print(i);
Serial.print(F("\t"));
Serial.println(valuesOfInterest[i]);
sum += valuesOfInterest[i];
cnt++;
i = (i + 1) % maxValues;
} while (cnt < nbValuesForAverage);
avg = (double) sum / nbValuesForAverage;
} else {
// array not full, we might not have enough values yet
int endAverageSumAt, nbValues;
endAverageSumAt = nextIndex;
if (nextIndex <= nbValuesForAverage) {
startAverageSumAt = 0;
nbValues = nextIndex;
} else {
startAverageSumAt = nextIndex - nbValuesForAverage;
nbValues = nbValuesForAverage;
}
for (int i = startAverageSumAt; i < endAverageSumAt; i++) {
Serial.print(i);
Serial.print(F("\t"));
Serial.println(valuesOfInterest[i]);
sum += valuesOfInterest[i];
}
avg = (double) sum / nbValues;
}
Serial.print(F("Average = ")); Serial.println(avg, 3); // 3 decimals
return avg;
}
void setup() {
Serial.begin(115200);
}
void loop() {
// perform our meaure and save the data in nextIndex position, then increment nextIndex
valuesOfInterest[nextIndex++] = analogRead(sensorPin);
if ((!arrayIsFull) && (nextIndex == maxValues)) { // we just filled in the last position
arrayIsFull = true;
}
if (nextIndex == maxValues) nextIndex = 0; // don't overflow
printAverageValue();
delay(1000);
}
hope this helps
rockwellramesha:
use statistics.h#include <Statistics.h>
Statistics stats(10);
...
You've been around for a while, you're not a complete noob. Please use code tags as you have previously been asked to do.
Do you really want to wait for 10 values? Or, do you not want to print anything until 10 or more values have been read?
I want to wait till 10 values and then calculate average. It prints the average whenever it is calculated.
I am trying to print it like this:
pdelta = 12 avg =0
pdelta = 14 avg =0
pdelta =16 avg =0
'
'
'
pdelta = 20 avg=some number
Because with the calculated average, I am going to open/close a solenoid. If the code starts calculating average right from the first value, the solenoid action also changes based on the average.
So I want to wait for 10 values, calculate average and then open/close solenoid.
So implement a counter to keep track of the number of values read. Not too difficult.
It will capture a new value every second, store it into the array and display the 5 indexes it uses to calculate the average and the average.
Thank you for the example. I ll try to implement it using the idea you gave.
dpoornima:
Thank you for the example. I ll try to implement it using the idea you gave.
if the function to calculate average you can see there is a test to see if you have enough values so may be change the function to not compute anything until you have enough values