smoothing example

I don’t understand this sketch, is the smoothing example.
I think this sketch send the value read divided by 10 to the computer

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer (as ASCII digits) 
  Serial.println(average, DEC);               
}

I will write like this if I want to have the avarage:

void loop() {
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings) {             
     
    index = 0;                           
    average = total / numReadings;         
    Serial.println(average, DEC);
    total=0
  }               
}

I’m rigth ?

That's one type of average, but the first is a rolling average.

davidino81: I don't understand this sketch,...

For numReadings = 10:

The sample sketch implements what Digital Signal Processing wonks call a "moving average." Each time through the loop it gets a new sample. The current sample replaces the oldest sample (ten samples ago) and the sketch prints the average of the most recent ten samples (including the current sample).

Your sketch goes through the loop 10 times, adding each sample into the total. After ten samples, it prints out the average of those ten samples and then starts on a whole new batch of ten samples.

Both have the characteristics of smoothing the input data. Use which seems appropriate to you. See Footnote.

Regards,

Dave

Footnote: Your scheme doesn't update the output as often, but it has the advantage of not requiring an array at all if you don't need the separate sample values for anything else. I mean, why did you store the value in an array?

I didn't know about rolling average ! Now everythig is clear. Thanks

more about different averages - http://en.wikipedia.org/wiki/Moving_average

Hello Everybody,

I am pretty new at arduino and currently i am working on project so i really need your help..

Here is my question ;

I have a sensor matrix that gives me 64 analog (voltage output) ..What I want to do is to make 10 readings of all 64 values and take the avarage value of them..so i want to see the avaraged 64 values on the secreen...

Thanks in advance..

and currently i am working on project

So where is your code? Summing a number of inputs is not a lot harder than summing a single input, but there may be complications that you haven't told us about (like needing a true rolling average, requiring more memory than you have available)

from sensor matrix (8*8) I get only 1 analog output (that gives me 64 different analog values differs between 0.2 V and 10 V) )which will be input(let say A0) for arduino -so what i want to do is simply take 10 readings (or in my case 10 scanning) and then take the average of all 64 values..finally i want to show the 64 averaged values..I do not have codes ,can i use the smoothing codes for this?

I hope i am clear enough..thanks

differs between 0.2 V and 10 V)

So you're going to need a potential divider to get that down under the processor's supply voltage. Then you need to write some code to control the input selector. After that, the code is smoothing code is pretty much as set out above.

Hello again,

Yeap I am aware of this ,i will simply put a voltage divider …so let me make the question deeper…

I have 88 pressure sensor matrix that (simply combining two mux) .I have designed a circuit that scans the martix and gives me 64 different voltage values between -0.02 / -10 V…I simply connect that output to Arduino (AO analog input pin) I want to read all those 64 values 10 times and then simply take averages of 64 values… the following code is not enough :S 1st iteration store 64 data,2nd iteration store 64 datas and so no–i guess i should have a table for 6410=640 datas ??

const int numReadings = 10;
 
 int readings[10];            // the readings from the analog input
 int index = 0;                  // the index of the current reading
 int total = 0;                  // the running total
 int average = 0;                // the average
int table[640];
   
 int inputPin = A0;
 
void setup()
 {
   // initialize serial communication with computer:
   Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int i = 0; i < numReadings; i++)
     readings[i] = 0;          
 }
 
void loop() {
   // subtract the last reading:
   total= total - table[index];         
  // read from the sensor:  
table[index] = analogRead(inputPin); 
  // add the reading to the total:
   total= total + table[index];       
  // advance to the next position in the array:  
   index = index + 1;                    
 
  // if we're at the end of the array...
   if (index >= numReadings)              
     // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
   average = total / numReadings;         
  // send it to the computer as ASCII digits
   Serial.println(average);   
  delay(1);        // delay in between reads for stability            
 }

Moderator edit: code tags

I don't understand why you have "readings" but don't use it, and don't use most of "table". Make "readings" a 64 x 10 array.

Thanks for your reply…

 #define NumOutput  64
#define NumReadings 10

int readings[NumOutput][NumReadings];      // the readings array from the analog input
int index = 0;                                             // the index of the current reading
float total = 0;                                          // the running total
float average[NumReadings];                      // the average
float totalAverage;
int i,j;

int inputPin = 0;

void setup()
{
  Serial.begin(9600);          // initialize serial communication with computer
  for(i=0;i<64;i++)
  {
    for(j=0;j<10; j++)
    {
     readings[i][j]= 0;     // Clear the data
    }
  }
}

void loop()
  {
  for(i=0; i<64; i++)        // This handles the data 
  {
    for(j=0; j<10 - 1; j++)
    {
      readings[i][j+1] = readings[i][j]; // Shift to make room for the new reading
    }
    readings[i][0] = analogRead(0); // get the latest value
  }
  
   for(i=0; i<64; i++)    
   {
     total = 0;                    // Reset the total for each sensor
     for(j=0; j<10; j++)
       total += readings[i][j];
     average[i] = total / 10;
   
   }
 
  total = 0;                 // Reset total again for use with totalAverage
 
  for(i=0; i<64; i++)  // Average all sensors together
  {
    total += average[i];
  }
  
  totalAverage = total / 10;
  Serial.println(totalAverage);       // send it to the computer (as ASCII digits)
}

that seems pretty close but i did not understand this part

void loop()
  {
  for(i=0; i<64; i++)        // This handles the data 
  {
    for(j=0; j<10 - 1; j++)
    {
      readings[i][j+1] = readings[i][j]; // Shift to make room for the new reading
    }
    readings[i][0] = analogRead(0); // get the latest value
  }
  
   for(i=0; i<64; i++)    
   {
     total = 0;                    // Reset the total for each sensor
     for(j=0; j<10; j++)
       total += readings[i][j];
     average[i] = total / 10;
   
   }
 
  total = 0;                 // Reset total again for use with totalAverage
 
  for(i=0; i<64; i++)  // Average all sensors together
  {
    total += average[i];
  }

…Thanks a ton in advance…

for(i=0;i<64;i++)
  {
    for(j=0;j<10; j++)
    {
     readings[i][j]= 0;     // Clear the data
    }
  }

Not necessary.

readings[i][j+1] = readings[i][j]; // Shift to make room for the new reading

I don't understand that either - there should be no reason to move that much data around. Keep a running total; for each new sample, subtract the oldest reading from the running total, and overwrite the oldest sample with the newest.