Cycles and average value
how write loop witch after x cycles adds values (for example sensor connected analog input) , calculate average ?
use loop, addition , division.
some things watch out include overflow in tot variable , division 0. change tot long, if needed. make sure cnt never 0.
you might want delay little in loop.
code: [select]
int val;
int tot = 0;
int ave;
int cnt = 5;
for(int i=0; i<cnt; i++) // loop cnt times
{
val = analogread(pin); // read value
tot += val; // sum values
}
ave = tot/cnt;some things watch out include overflow in tot variable , division 0. change tot long, if needed. make sure cnt never 0.
you might want delay little in loop.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Cycles and average value
arduino
Comments
Post a Comment