For the software, my main goal is to modulize the requests or organization for information into separate functions. One function receives the current value from the sensor. A second function calls the initial function, and places the value into an array for the number of times, variable sampleSize. A third function iterates through the array and returns the mean of all values. The different function can be called to view the sample value in an organized table.
Code:
//Simple Data Average
static const int sampleSize = 160;
int readings[sampleSize];
int sampleIndex = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
//straightData();
//buildArray();
//printArray();
meanArray();
delay(10);
}
// Averages samples array to output mean
int meanArray(){
int mean = buildArray();
mean = mean / sampleSize;
Serial.print("Mean: ");
Serial.println(mean);
return mean;
}
// Prints readings array in a nice format
void printArray(){
buildArray();
for(int i = 0; i<sampleSize; i++){
Serial.print("||");
Serial.print(readings[i]);
}
Serial.println();
}
// Fills readings array with data from the sensor
int buildArray() {
int sum = 0;
for (int i = 0; i < sampleSize; i++){
readings[i] = straightData();
sum += readings[i];
}
return sum;
}
// Live feed of data from sensor
int straightData(){
int tempIn = analogRead(A6);
//Serial.println(tempIn);
return tempIn;
}