SUM and average

Hi I need do a basic sum and average from a force sensor.
Im struggeling to find a tutorial fo this
I donot want a rolling average , just sum 10 readings and divide by 10 spit out the result and loop again, if some one could point me towards a tutorial on this it would be great.
regards
Hanica

Do you really need a tutorial ?

set counter to zero
set total to zero
start of loop
  take a reading
  add reading to total
  increment counter
  if counter equals 10
    divide total by 10 to get average
    use the average
    set counter to zero
    set total to zero
  end if
end of loop

something like this? (if you want to keep the samples values as well for other maths)

const size_t maxSamples = 10;
int samples[maxSamples];
long samplesSum = 0;

for (size_t sampleIndex = 0; sampleIndex < maxSamples; sampleIndex++) {
  samples[sampleIndex] = analogRead(forceSensorPin); 
  samplesSum += samples[sampleIndex];
  delay(100);
} 

double average = (double) samplesSum / (double) maxSamples; // or just as integer if you don't care about decimals

See this webpage:

It is the syntax that I need , I.E. what is written in setup etc.

What is your level of experience in programming the Arduino ?

Have you looked at the example sketches that ship with the IDE?

my experiance is quite basic, mostly making stepper motors move and simple automation.
I will have a look at the smoothing code that might be what im after.
sometimes its amatter of knowing what to put in the search !!
Thanks

Moving Average Example

Make sure you don't exceed the maximum range of your sum/total variable (i.e. type int can't go over 32,767).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.