Hi everyone,
Quick background on my project: I am measuring the resistance of 5 to 10 electrical components at once and tracking their resistance change over the course of one week. For each device, I am taking 10000 measurements (from the analog in pins) over the course of ~1 minute, and recording the average. I am calculating the average by simply summing the measurements and dividing by 10000. This setup has been working well, however, I would like to also record the standard deviation of the 10000 measurements for each sample.
I have added this statistics library- Arduino Playground - Statistics
This works well if I only test 1 device at a time, but I would like to run up to 10 at a time. Can this library run more than one sample at once? Or is the another method to do this?
Any kind of help would be appreciated. The easier to implement the better!
For reference, here is an example of how I am averaging the data. I understand I will probably need to completely restructure it.
const int flipCount = 10000; //number of measurements to average
float voltageA0Total = 0;
float voltageA1Total = 0;
float vA0Avg = 0 ;
float vA1Avg = 0 ;
int counter = 0 ;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.print ("time(s)");
Serial.print (" vA0avg");
Serial.println (" vA1avg");
}
void loop() {
float voltageA0 = analogRead(A0) * (5.0 / 1023.0);
voltageA0Total = voltageA0Total + voltageA0 ;
float voltageA1 = analogRead(A1) * (5.0 / 1023.0);
voltageA1Total = voltageA1Total + voltageA1 ;
counter = counter +1 ;
delay (6);
if (counter > flipCount) {
vA0Avg = voltageA0Total / counter ;
vA1Avg = voltageA1Total / counter ;
Serial.print (millis()/1000);
Serial.print (" ");
Serial.print (vA0Avg);
Serial.print (" ");
Serial.println (vA1Avg);
counter = 0;
voltageA0Total = 0;
voltageA1Total = 0;
}
}
system
April 18, 2018, 4:09pm
2
It looks like the library is small enough that you can create multiple instances of the class - one for each pin.
Ahh, I'm a dummy. Thanks for helping out!
For future reference, here is how I did it. Once you have the statistics library added it is very simple.
#include "Statistic.h" // without trailing s
Statistic a0;
Statistic a1;
Statistic a2;
Statistic a3;
Statistic a4;
Statistic a5;
Statistic a6;
Statistic a7;
const int flipCount = 10000;
void setup(void)
{
Serial.begin(9600);
void clear(); //explicitly start clean
Serial.print ("time(s) ");
Serial.print ("a0AVG ");
Serial.print ("a1AVG ");
Serial.print ("a2AVG ");
Serial.print ("a3AVG ");
Serial.print ("a4AVG ");
Serial.print ("a5AVG ");
Serial.print ("a6AVG ");
Serial.print ("a7AVG ");
Serial.print ("a0StDev ");
Serial.print ("a1StDev ");
Serial.print ("a2StDev ");
Serial.print ("a3StDev ");
Serial.print ("a4StDev ");
Serial.print ("a5StDev ");
Serial.print ("a6StDev ");
Serial.println ("a7StDev ");
}
void loop(void)
{
//measure voltage for each pin
float vA0 = analogRead(A0) * (5.0 / 1023.0);
float vA1 = analogRead(A1) * (5.0 / 1023.0);
float vA2 = analogRead(A2) * (5.0 / 1023.0);
float vA3 = analogRead(A3) * (5.0 / 1023.0);
float vA4 = analogRead(A4) * (5.0 / 1023.0);
float vA5 = analogRead(A5) * (5.0 / 1023.0);
float vA6 = analogRead(A6) * (5.0 / 1023.0);
float vA7 = analogRead(A7) * (5.0 / 1023.0);
//add measured voltage to database for each pin
a0.add(vA0);
a1.add(vA1);
a2.add(vA2);
a3.add(vA3);
a4.add(vA4);
a5.add(vA5);
a6.add(vA6);
a7.add(vA7);
//delay (1);
if (a0.count() == flipCount)
{
Serial.print (millis()/1000); Serial.print(" ");
Serial.print(a0.average(), 4); Serial.print(" ");
Serial.print(a1.average(), 4); Serial.print(" ");
Serial.print(a2.average(), 4); Serial.print(" ");
Serial.print(a3.average(), 4); Serial.print(" ");
Serial.print(a4.average(), 4); Serial.print(" ");
Serial.print(a5.average(), 4); Serial.print(" ");
Serial.print(a6.average(), 4); Serial.print(" ");
Serial.print(a7.average(), 4); Serial.print(" ");
Serial.print(a0.pop_stdev(), 4); Serial.print(" ");
Serial.print(a1.pop_stdev(), 4); Serial.print(" ");
Serial.print(a2.pop_stdev(), 4); Serial.print(" ");
Serial.print(a3.pop_stdev(), 4); Serial.print(" ");
Serial.print(a4.pop_stdev(), 4); Serial.print(" ");
Serial.print(a5.pop_stdev(), 4); Serial.print(" ");
Serial.print(a6.pop_stdev(), 4); Serial.print(" ");
Serial.print(a7.pop_stdev(), 4); Serial.print(" ");
Serial.println();
a0.clear();
a1.clear();
a2.clear();
a3.clear();
a4.clear();
a5.clear();
a6.clear();
a7.clear();
}
}
void clear(); //explicitly start clean
This declares a function called clear() which will only be known in the setup() function. It does not call it and you don't have a clear() function in your code.
Pete
system
April 18, 2018, 8:35pm
6
You've never heard of arrays? An array of pin numbers and an array of Statistics objects, and you'd have less than half as much code.